AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-169992

Lance's questions

Martin Hope
Lance Pollard
Asked: 2025-04-25 15:32:42 +0800 CST

如何创建一个仅具有所有偶数或所有奇数行大小且仅变化 2 的布局?

  • 8

我已经连续几天使用 Claude 3.7 Sonnet 和 ChatGPT 4o 进行迭代,试图让它完全符合我的要求,但它在算法上不断犯错误,并且基本上循环往复地犯同样的错误。

如何制作一个“布局”(整数数组),它代表一个网格,具有以下约束:

  1. 该函数是generateGridLayout(n, minColumns, maxColumns),实际上它通常被称为gen(n, 3, 7),但理论上可以是任何可以形成漂亮网格的东西,比如gen(n, 2, 256)最大范围。
  2. 它应该处理任意数量的元素(比如最多Number.MAX_SAFE_INTEGER,也就是9007199254740991,但实际上我的“网格布局”主要只有最多 1000 个项目)。
  3. 如果项目数量n为奇数,则每行只能包含奇数个值。如果项目n数量为偶数,则每行可以包含偶数或奇数个值(例如,30,可以是 10 行,每行 3 个值,或者 3 行,每行 10 个值)。
  4. 行数之间的差异只能是2,并且始终是递减的。差异不能是 3、4 等等。这意味着 7 不能是 [5, 2] 或 [4, 4, 1],因为它们的跳跃次数大于 2。如果gen(7, 3, 7),它只能是 [7] 或 [3, 3, 1]。
  5. (元注释,这是一个 UI 布局,因此它基于视口/容器大小,所以如果我们说“最大值是 7”,但只有 5 个空间,它会将最大值设置为 5,但这个事实与解决方案并不相关)
  6. 如果我们说“最大值为 7”,但项目数为偶数,并且偶数不能满足“所有偶数行或所有奇数行”的条件,则尝试maxColumns - 1,依此类推,直到minColumns。
  7. 重要提示:它应该最小化小行的数量。因此,对于 29,当最大列数为 6 时,它应该是 [5, 5, 5, 5, 5, 3, 1],而不是 [5, 5, 5, 5, 3, 3, 3]。也就是说,它最大化了大行的数量,并最小化了小行的数量。同样,对于 29,它绝对不应该是 [5, 5, 5, 5, 3, 3, 1, 1, 1]。

以下是一些示例来说明该目标:

31
[5, 5, 5, 5, 5, 3, 3]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1]

30
[5, 5, 5, 5, 5, 5]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

29
[5, 5, 5, 5, 5, 3, 1]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1]

28
[6, 6, 6, 6, 4]
[4, 4, 4, 4, 4, 4, 4]

27
[5, 5, 5, 5, 3, 3, 1]
[3, 3, 3, 3, 3, 3, 3, 3, 3]

26
[6, 6, 6, 4, 2, 2]
[4, 4, 4, 4, 4, 4, 4]

23
[5, 5, 5, 5, 3]
[3, 3, 3, 3, 3, 3, 3, 1, 1]

这些显示,如果 5 或 6 是最大列,它会做什么,以及如果 4 或 3 是最大列,它应该做什么。

例如,26 个最大 7 列不应该是:

[6, 6, 6, 6, 2] # jumps more than 2
[4, 4, 4, 4, 4, 4, 2] # doesn't maximize maxColumns

理想情况下应该是:

[6, 6, 6, 4, 2, 2] # jumps max 2, maximizes large columns, minimizes small columns.

这是我当前的解决方案:

log(29, 2, 7)
log(29, 2, 6)
log(29, 2, 5)
log(29, 2, 4)
log(44, 2, 3)

function distributeGridLayout(
  length,
  minColumns,
  maxColumns
) {
  function recur(
    dp,
    length,
    width,
  ) {
    if (length == 0) {
      return []
    }

    if (length < width - 2 || width <= 0) {
      return
    }

    if (dp[width].has(length)) {
      return
    }

    dp[width].add(length)

    for (let i = 0; i < 2; i++) {
      let result = recur(dp, length - width, width)
      if (result) {
        return [width, ...result]
      }
      width -= 2
    }

    return
  }

  if (length <= maxColumns) {
    return [length]
  }

  if (maxColumns >= 3 && length === 7) {
    return [3, 3, 1]
  }

  if (maxColumns >= minColumns && length % maxColumns === 0) {
    const result = []
    while (length) {
      result.push(maxColumns)
      length -= maxColumns
    }
    return result
  }

  if (maxColumns > 4) {
    if (maxColumns > minColumns && length % (maxColumns - 1) === 0) {
      const result = []
      maxColumns--
      while (length) {
        result.push(maxColumns)
        length -= maxColumns
      }
      return result
    }
  }

  const dec = 2 - (length % 2)

  maxColumns -= maxColumns % dec

  const dp = Array.from(
    { length: maxColumns + 1 },
    () => new Set(),
  )

  for (let width = maxColumns; width > 0; width -= dec) {
    const result = recur(dp, length - width, width)
    if (result) {
      if (width <= minColumns) {
        return
      }
      return [width, ...result]
    }
  }

  return
}

function log(n, min, max) {
  const grid = distributeGridLayout(n, min, max)
  console.log(`gen(${n}, ${min}, ${max})`, grid)
}

这对大多数人来说都是有效的,比如这个藏文布局(29 个字符):

但它不适用于这种泰语布局(44 个字符,2-3 列),这是它的结尾(在我的 UI 中,如果算法返回未定义,它会回退到基本的网格布局):

我到底需要修改什么才能让它始终符合我的规则?44 布局(3 最大 2 最小)基本上应该是一个 2 列布局……

javascript
  • 1 个回答
  • 85 Views
Martin Hope
Lance Pollard
Asked: 2024-10-18 19:29:33 +0800 CST

如何构建一个 trie 来查找精确的语音匹配,按权重进行全局排序并分页?(基于此示例)

  • 0

目标

我在使用 AI 构建用于查找押韵词的 Trie 方面取得了很大进展。基本上,假设您有 1000 万个使用CMU 发音系统编写的英语单词,其中每个音素都是 Trie 中的一个节点(ARPABET在此图中)。

在此处输入图片描述

以下是主要特点:

  1. Trie 节点是 1+ ASCII 符号。例如B“b”音和CH“ch”音等。
  2. Trie 可以分页并从特定页面跳转。因此它需要一个limit, 和page属性。
  3. 分页可以选择性地限制为特定音素序列长度的匹配。例如,音素的个数g l a d为 4。
  4. Trie 搜索输入(由用户提供)是音素数组。音素是 ASCII 中的声音单位(如上所示ARPABET)。
  5. 输入到 trie 中会扩展为所有可能的韵律。它使用音素到音素数组的映射进行扩展(仅在下面的函数中部分实现,需要数周时间才能正确微调此处的值,我很快就需要 TODO)。
  6. 我们可以将扩展的输入称为“ rhymingPhonemeArray”。
  7. 中的每个音素序列都rhymingPhonemeArray有权重。所谓“加权”,这个权重基本上就是“映射的音素与原始音素的接近程度”。这样我们就可以判断“这个音素序列(具有较低的累积权重)比这个音素序列押韵更好rhymingPhonemeArray”。

问题

我现在面临的问题(我们找到的解决方案,下面用 JavaScript 分享)是:

  1. 遍历整个 trie 来查找所有可能的匹配,这是未优化的。理想情况下,它只遍历它需要的内容(页面/限制数量和偏移量)。
  2. 之后 Trie 会对整个匹配集进行排序,而不是按照我们的要求排序,也就是获取已排序的页面/限制数量。这是关键问题,不确定是否/如何以优化的方式做到这一点,或者是否可能。
  3. 是rhymingPhonemeArray迭代的,所以如果我们分页并且rhymingPhonemeArray是这样的[G-A-D (cum-weight: 10), G-L-A-D (cum-weight: 24 or whatever), G-R-A-D (cum-weight: 29), etc.],你会G-A-D先找到与押韵的所有内容,然后再分页,分页G-L-A-D等等。我想避免这种分组。相反,累积权重需要通过所有 10m 个单词的“全局集”进行排序和分页。

因此对于 (3),它应该找到(类似这样的内容):

input: G-A-D
matches:
  G-A-D
  G-L-A-D
  A-G-A-D
  A-R-G-A-D
  G-R-A-D
  A-G-R-A-D
  A-R-G-L-A-D
  A-R-G-R-A-D
  ...

我说的“类似这样的事情”的意思是,请注意它并不像这样(首先,G-A-D找到所有匹配项,然后G-L-A-D找到所有匹配项等等):

input: G-A-D
matches:
  G-A-D
  A-G-A-D
  A-R-G-A-D
  G-L-A-D
  A-R-G-L-A-D
  G-R-A-D
  A-G-R-A-D
  A-R-G-R-A-D
  ...

相反,在第一种情况下matches,它更加交织在一起,应该基于每个单词的全局累积权重。

问题

如何修改以下“Trie 实现”来解决上述 1、2 和 3 的问题? 注意:它不需要 100% 准确,只需寻找如何正确解决此分页问题的关键见解(即使只是在高级/伪代码级别)。它们都是同一潜在问题的各个方面,这是我已经说过的,但我会再次说明:

下面的 Trie 实现不能正确且有点高效地通过按全局累积权重排序的词进行分页,这些词与rhymingPhonemeArray(从输入生成的phonemes)完全匹配。

有没有可能解决这个问题,而不必遍历整个 Trie?考虑到(记住),输入被扩展为rhymingPhonemeArray(可能有很多种可能性,但我们实际上会将输入限制为 3 个音节,这与主题无关)。如果不可能,你能解释一下为什么不可能吗?

如果可能的话,您将如何修改此 trie 以支持分页并跳转到特定页面,而不必遍历所有内容,同时分页结果根据每个单词的累积权重进行全局排序?

Trie 实现

我们最终得到的 Trie 如下:

class TrieNode {
  constructor() {
    this.children = {}; // Store child nodes
    this.isWord = false; // Flag to check if node marks the end of a word
    this.word = null; // Store the word if this is an end node
    this.cumulativeWeight = 0; // Store the cumulative weight for sorting
    this.phonemeLength = 0; // Length of the phoneme sequence
  }
}

class PhoneticTrie {
  constructor() {
    this.root = new TrieNode(); // Root node of the trie
  }

  // Insert words and phoneme clusters into the Trie
  insert(word, phonemes, weight) {
    let node = this.root;
    for (let phoneme of phonemes) {
      if (!node.children[phoneme]) {
        node.children[phoneme] = new TrieNode();
      }
      node = node.children[phoneme];
    }
    node.isWord = true;
    node.word = word;
    node.cumulativeWeight = weight; // Store the cumulative weight at this node
    node.phonemeLength = phonemes.length; // Store the length of the phoneme sequence
  }

  // Global sorting of rhyming phoneme matches using a min-heap
  searchWithPagination({ phonemes, limit, page, length }) {
    const minHeap = new MinHeap(); // Min-Heap to store sorted results

    // Generate rhyming phoneme variations
    const rhymingPhonemesArray = expandToRhymingPhonemes(phonemes);

    // Search the Trie for all rhyming phoneme sequences and insert results into the global heap
    for (let rhyme of rhymingPhonemesArray) {
      this.searchAndInsertToHeap(rhyme.phonemes, this.root, rhyme.weight, minHeap, length);
    }

    // Paginate results directly from the globally sorted heap
    const paginatedResults = [];
    const startIndex = (page - 1) * limit;
    let index = 0;

    while (minHeap.size() > 0 && paginatedResults.length < limit) {
      const wordData = minHeap.extractMin();
      if (index >= startIndex) {
        paginatedResults.push(wordData.word);
      }
      index++;
    }

    return paginatedResults;
  }

  // Search a specific phoneme sequence in the Trie and insert matches into the heap
  searchAndInsertToHeap(phonemes, node, rhymeWeight, heap, targetLength, depth = 0, phonemeSeq = []) {
    if (depth === phonemes.length) {
      if (node.isWord && node.word) {
        // If length filtering is specified, ensure the word matches the phoneme length
        if (!targetLength || node.phonemeLength === targetLength) {
          heap.insert({
            word: node.word,
            cumulativeWeight: node.cumulativeWeight + rhymeWeight, // Add the rhyme weight here
            phonemeLength: node.phonemeLength, // Include phoneme length for sorting
          });
        }
      }
      return;
    }

    const phoneme = phonemes[depth];
    if (!node.children[phoneme]) return; // No match
    this.searchAndInsertToHeap(phonemes, node.children[phoneme], rhymeWeight, heap, targetLength, depth + 1, [...phonemeSeq, phoneme]);
  }
}


class MinHeap {
  constructor() {
    this.heap = []; // Store the heap elements
  }

  // Insert an item into the heap based on its weight and phoneme length
  insert({ word, cumulativeWeight, phonemeLength }) {
    this.heap.push({ word, cumulativeWeight, phonemeLength });
    this.bubbleUp(this.heap.length - 1);
  }

  bubbleUp(index) {
    let currentIndex = index;
    while (currentIndex > 0) {
      const parentIndex = Math.floor((currentIndex - 1) / 2);
      // Sort primarily by cumulative weight, secondarily by phoneme length
      if (
        this.heap[currentIndex].cumulativeWeight > this.heap[parentIndex].cumulativeWeight ||
        (this.heap[currentIndex].cumulativeWeight === this.heap[parentIndex].cumulativeWeight &&
          this.heap[currentIndex].phonemeLength > this.heap[parentIndex].phonemeLength)
      ) {
        break;
      }
      [this.heap[currentIndex], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[currentIndex]];
      currentIndex = parentIndex;
    }
  }

  // Extract the item with the lowest weight
  extractMin() {
    if (this.heap.length === 0) return null;
    if (this.heap.length === 1) return this.heap.pop();
    const min = this.heap[0];
    this.heap[0] = this.heap.pop();
    this.bubbleDown(0);
    return min;
  }

  bubbleDown(index) {
    const lastIndex = this.heap.length - 1;
    while (true) {
      let leftChildIdx = 2 * index + 1;
      let rightChildIdx = 2 * index + 2;
      let smallestIdx = index;

      if (
        leftChildIdx <= lastIndex &&
        (this.heap[leftChildIdx].cumulativeWeight < this.heap[smallestIdx].cumulativeWeight ||
          (this.heap[leftChildIdx].cumulativeWeight === this.heap[smallestIdx].cumulativeWeight &&
            this.heap[leftChildIdx].phonemeLength < this.heap[smallestIdx].phonemeLength))
      ) {
        smallestIdx = leftChildIdx;
      }
      if (
        rightChildIdx <= lastIndex &&
        (this.heap[rightChildIdx].cumulativeWeight < this.heap[smallestIdx].cumulativeWeight ||
          (this.heap[rightChildIdx].cumulativeWeight === this.heap[smallestIdx].cumulativeWeight &&
            this.heap[rightChildIdx].phonemeLength < this.heap[smallestIdx].phonemeLength))
      ) {
        smallestIdx = rightChildIdx;
      }
      if (smallestIdx === index) break;

      [this.heap[index], this.heap[smallestIdx]] = [this.heap[smallestIdx], this.heap[index]];
      index = smallestIdx;
    }
  }

  size() {
    return this.heap.length;
  }
}

function expandToRhymingPhonemes(phonemes) {
  // todo: this function would have a huge map of phoneme substitutions...
  const phonemeSubstitutions = {
    "k": ["g", "p"], // Example of substitutions for the phoneme "k"
    "æ": ["a", "e"], // Example for "æ"
    "t": ["d", "s"], // Example for "t"
  };

  const rhymingPhonemesArray = [];

  function generateRhymes(sequence, depth = 0, currentWeight = 0) {
    if (depth === sequence.length) {
      rhymingPhonemesArray.push({ phonemes: sequence.slice(), weight: currentWeight });
      return;
    }

    const phoneme = sequence[depth];
    const substitutions = phonemeSubstitutions[phoneme] || [phoneme];

    for (const sub of substitutions) {
      generateRhymes(
        [...sequence.slice(0, depth), sub, ...sequence.slice(depth + 1)],
        depth + 1,
        currentWeight + (sub === phoneme ? 0 : 1)  // Substitution adds to weight
      );
    }
  }

  generateRhymes(phonemes);
  return rhymingPhonemesArray;
}

const trie = new PhoneticTrie();
trie.insert("glad", ["g", "l", "a", "d"], 3);
trie.insert("grad", ["g", "r", "a", "d"], 2);
trie.insert("blad", ["b", "l", "a", "d"], 4);
trie.insert("grin", ["g", "r", "i", "n"], 5);

// Search for similar words to "g-l-a-d" and paginate the results, with optional length filtering
const resultsPage1 = trie.searchWithPagination({
  phonemes: ["g", "l", "a", "d"],
  limit: 2,
  page: 1,
  length: 4, // Only consider words with exactly 4 phonemes
});

const resultsPage2 = trie.searchWithPagination({
  phonemes: ["g", "l", "a", "d"],
  limit: 2,
  page: 2,
  length: 4,
});

console.log(resultsPage1); // Output: ["grad", "glad"] (sorted by weight and length)
console.log(resultsPage2); // Output: ["blad", "grin"]

更新

  1. 这里是对 Trie 进行进一步扩展/尝试以解决这一关键问题的要点。
javascript
  • 1 个回答
  • 46 Views
Martin Hope
HareSurf
Asked: 2024-09-17 22:26:23 +0800 CST

如何以尽可能紧凑的方式计算列数,使得每一行都包含偶数或奇数个项目?

  • 4

我想要布局与此类似的unicode字形:

所展示的集合 (字母表、辅音字母、元音附标文字、音节文字等) 可能有 13 到 100 个项目,因此可能有一组包含 99 个项目,或者一组包含 16 个项目,等等。

如何才能最美观地(自动地)布局元素(以便在响应时也能很好地布局)?这是我默认使用 flexbox 的情况:

在此处输入图片描述

每个项目都有固定的高度,并且宽度是响应的(但每个元素都相同)。

目标是让每行包含偶数或奇数个项目。这样所有垂直列都会对齐(并且不会像我的动画 gif 那样,偶数行/奇数行混杂在一起)。

  1. 是否可以用数学公式来计算“每行需要 5 个项目,但最后一行有 3 个(或 1 个)”之类的东西?给出总数,例如 99?

您可能会怎样做呢?

我有一个 React 组件,它执行 Flexbox 布局,例如<Grid minWidth={96} gap={8} maxColumns={5} breakpoints={[5, 3, 1]} />,其中breakpoints是每行允许的项目数,maxColumns是最大列数(断点/maxColumn 有点多余,您可以定义其中一个,也许我稍后会清理它)。所有这些信息可能都不相关,但以防万一,这只是我的响应式网格组件。

我怎样才能以某种方式将总数除以 x来确定它应该是偶数行还是奇数行,然后将maxColumns或设置breakpoints为“最紧凑”的值?例如:

  • 如果有26 个元素,则最好是 6 行,每行 4 个,然后是 1 行,每行 2 个。- 如果有27 个元素,

它的“美”可能是主观的,并没有明确的规则来定义什么是最美的。只是寻找那些不容易在末尾留下孤立节点,然后在所有其他行上都有 7 个项目的元素。理想情况下,我们有:

  • 3 至 7 列之间。
  • 最后一行的项目数必须与所有前一行相同,或者不少于maxRowCount - 2(因此,如果所有行都有 7 个,则最后一行只能有 5 个或 7 个,或者如果所有行都有 5 个,则最后一行只能有 3 个或 5 个,等等)。
  • 行项目有一个minWidth: 96px,并会增长以填充空间。一旦我们掌握了此工作原理的基本知识,我就可以弄清楚如何使事物具有响应性,但假设存在containerWidth。

所以在我的脑海里,假设我们有containerWidth === 700px。然后,,700/96 == 7+所以每行可能有 7 个。如果我们有 99 个项目(例如),那么99 % 7 == 1,所以7不会起作用,因为最后一行会有 1 个。6不会起作用,因为99是偶数,所以尝试5? 99 % 5 == 4,那不会起作用,因为4是偶数。所以尝试3,,99 % 3 == 0这样就可以按原样工作,使用它!

您将怎样实现这样的目标?

javascript
  • 2 个回答
  • 31 Views
Martin Hope
Lance
Asked: 2024-06-25 10:50:33 +0800 CST

Keyman“键盘文件是旧版本”以及 Keyman CLI 的其他错误/警告?

  • 5

我使用人工智能来帮助我学习 Keyman,结果是这样的:

c My Fantasy Script Keyboard
store(&VERSION) '1.0'
store(&NAME) 'My Fantasy Script'
store(&BITMAP) 'fntsy.ico'
store(&LAYOUTFILE) 'fntsy.kmn'
store(&TARGETS) 'windows macosx linux web'

begin Unicode > use(main)

group(main) using keys
+ '0' > U+00A1 c Mapping for the '0' key
+ '1' > U+00A6 c Mapping for the '1' key
+ '2' > U+00A2 c Mapping for the '2' key
+ '3' > U+00A3 c Mapping for the '3' key
+ '4' > U+00A5 c Mapping for the '4' key
+ '5' > U+00A4 c Mapping for the '5' key
+ '6' > U+00A9 c Mapping for the '6' key
+ '7' > U+00A7 c Mapping for the '7' key
+ '8' > U+00AB c Mapping for the '8' key
+ '9' > U+00AE c Mapping for the '9' key

我究竟做错了什么?

为什么运行此命令(安装@keymanapp/kmc )时会出现此输出?

$ kmc build --color --debug fntsy.kmn
fntsy.kmn - info KM05002: Building fntsy.kmn
fntsy.kmn:2 - warn KM02081: The keyboard file is an old version
fntsy.kmn:2 - error KM0200D: Invalid 'version' command
fntsy.kmn - info KM05007: fntsy.kmn failed to build.

我该如何修复这个问题?文档很难理解,也很难找到解决方案,所以我可能使用了错误版本的文件格式(虽然我找不到正确的版本,也不知道我的版本出了什么问题),而且无效的版本也让我很困惑。

更新:

将版本更改为参考 Keyman 版本(不是我的语言版本)似乎可以解决这个问题:

store(&VERSION) '17.0'

但现在我收到了许多这样的警告:

fntsy.kmn:11 - warn KM02084: There are too many characters in the keystroke part of the rule.
fntsy.kmn:12 - warn KM02084: There are too many characters in the keystroke part of the rule.

我有这样的规则,有什么问题?

group(main) using keys
+ 'I$&--@_^' [K_TAB] > 'I%9440005' c Mapping for the 'I$&--@_^' key, the vowel i with stress, lengthening, tense, stress, nasalization and extra low tone
+ 'I$&-@_^' [K_TAB] > 'I%940005' c Mapping for the 'I$&-@_^' key, the vowel i with stress, lengthening, tense, stress, nasalization and low tone
+ 'I$&++@_^' [K_TAB] > 'I%9330005' c Mapping for the 'I$&++@_^' key, the vowel i with stress, lengthening, tense, stress, nasalization and extra high tone
+ 'I$&+@_^' [K_TAB] > 'I%930005' c Mapping for the 'I$&+@_^' key, the vowel i with stress, lengthening, tense, stress, nasalization and high tone
keyman
  • 1 个回答
  • 16 Views
Martin Hope
Lance
Asked: 2024-05-27 11:02:24 +0800 CST

NPM shasum、完整性和签名字段之间的区别和用途是什么?

  • 4

NPM 包元数据 JSON 示例如下所示(dist移至顶部):

{
  "name": "lodash",
  "version": "4.17.21",
  "description": "Lodash modular utilities.",
  "dist": {
    "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
    "shasum": "679591c564c3bffaae8454cf0b3df370c3d6911c",
    "tarball": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
    "fileCount": 1054,
    "unpackedSize": 1412415,
    "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgMS3ZCRA9TVsSAnZWagAA8+4P/jx+SJ6Ue5oAJjz0L7gw\nLDD5YvP8aoliFq4GYkwUXfVQvOwomIPfa+U5Kao/hDfuwFQ/Bq5D5nSsl2bj\nrjJgvlKXna0SId8AgDgY2fB7zSfninuJvalY4iTWMN8DFSpG0XE2QFfoKpd3\njDmuzcNtgr79QV6DgjOVkHiP1IGNDlLTc1QEKiwo/5CdGQi1q/iCj6dViQMJ\nByuuuV2Qzi3f/FI25cG797WZar1MHhhlcnB50HiVBGp54IZOyuqdqWPduZQo\nvhONtonxPGBm3/J+uAkeUSSyL3Ud+FzLvdg8WEI9gDL0yvU4k0FcsnOONEYn\nngLaKEsw2xAnPBYW3Lf73Jnpwx6FAT3k49kgzxiNYSxEo7x4wiuNtBoDMyNw\nEKj6SZ0bUNmaJgiMfDnnDjCKjI3JrO1hho8z6CkwuvxuWLlW9wSsVayggzAI\nEhfeTeISugVHh332oDY2MI/Ysu8MnVN8fGmqeYQBBFj3aWatuA2NvVjACnX/\n54G7FtCU8TxZpm9shFRSopBx8PeI3r+icx1CT8YVFypY416PLnidHyqtME1G\neuRd1nWEz18hvVUAEHmuvHo+EPP3tITmTTUPQcZGMdBcZC+4UBmPMWX466HE\nbHw4aOnUWMa0sWfsERC5xzRZAb4lgMPEoTOnZyN4usMy7x9TzGZKZvU24HUE\nmpae\r\n=NOmG\r\n-----END PGP SIGNATURE-----\r\n",
    "signatures": [
      {
        "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
        "sig": "MEUCIF3Yithbtmy1aEBNlfNWbLswAfPIyQUuNUGARD3Ex2t4AiEA6TlN2ZKJCUpS/Sf2Z6MduF1BNSvayHIpu5wAcICcKXw="
      }
    ]
  },
  "keywords": [
    "modules",
    "stdlib",
    "util"
  ],
  "homepage": "https://lodash.com/",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/lodash/lodash.git"
  },
  "icon": "https://lodash.com/icon.svg",
  "license": "MIT",
  "main": "lodash.js",
  "author": {
    "name": "John-David Dalton",
    "email": "[email protected]"
  },
  "contributors": [
    {
      "name": "John-David Dalton",
      "email": "[email protected]"
    },
    {
      "name": "Mathias Bynens",
      "email": "[email protected]"
    }
  ],
  "scripts": {
    "test": "echo \"See https://travis-ci.org/lodash-archive/lodash-cli for testing details.\""
  },
  "gitHead": "c6e281b878b315c7a10d90f9c2af4cdb112d9625",
  "bugs": {
    "url": "https://github.com/lodash/lodash/issues"
  },
  "_id": "[email protected]",
  "_nodeVersion": "14.15.5",
  "_npmVersion": "6.14.11",
  "_npmUser": {
    "name": "bnjmnt4n",
    "email": "[email protected]"
  },
  "directories": {

  },
  "maintainers": [
    {
      "name": "mathias",
      "email": "[email protected]"
    },
    {
      "name": "jdalton",
      "email": "[email protected]"
    },
    {
      "name": "bnjmnt4n",
      "email": "[email protected]"
    }
  ],
  "_npmOperationalInternal": {
    "host": "s3://npm-registry-packages",
    "tmp": "tmp/lodash_4.17.21_1613835736675_0.01913912595366596"
  },
  "_hasShrinkwrap": false
}

以下是我与这 3 个领域相关的问题:

  • dist.shasum:据说这是shasum <tarball>tarball(CLI 命令)的,使用 SHA-1。
  • dist.integrity:ChatGPT 说(我认为...)这与 具有相同的目的shasum,但“更安全”(因为有时它使用 SHA-256 或 SHA-512 编码。但是两者的真正区别或原因是什么dist.shasum和dist.integrity?它们还有其他区别吗?
  • dist.signatures[*].sig: 这个签名有什么作用?你如何使用它?ChatGPT 似乎说这是使用公钥进行测试的,但是谁拥有密钥的哪一面,何时使用?你什么时候/为什么会有多个?

dist.npm-signature既然我们也在这里,那么与上述三件事有何关系?

security
  • 1 个回答
  • 20 Views
Martin Hope
Lance
Asked: 2024-04-07 05:25:48 +0800 CST

如何在到达 0 链后停止遍历位(方向 = 从最低到最高有效位)?

  • 3

我有这两个函数来遍历 TypeScript 中整数的 32 位:

export function* walkBitsFromLSB(n: number) {
  for (let i = 0; i < 32; i++) {
    // Check the least significant bit (LSB)
    const bit = (n >> i) & 1
    yield bit
  }
}

export function* walkBitsFromMSB(n: number) {
  // Start with a mask that isolates the MSB of a 32-bit integer
  let mask = 1 << 31
  for (let i = 0; i < 32; i++) {
    // Apply the mask to n and check if the result is non-zero
    const bit = n & mask ? 1 : 0
    yield bit
    // Shift the mask right for the next bit
    mask >>>= 1
  }
}

我可以修改 MSB 使其仅遍历“相关”位,如下所示:

export function* walkRelevantBitsFromMSB(n: number) {
  let mask = 1 << 31;
  let start = false; // Flag to indicate when we've found the first non-zero bit
  for (let i = 0; i < 32; i++) {
    const bit = n & mask ? 1 : 0;
    if (bit === 1) start = true;
    if (start) yield bit; // Only start yielding bits after the first 1 is encountered
    mask >>>= 1;
  }
}

如何以某种最佳方式对LSB函数执行相同的操作?

也就是说,如果这些位是0b00000000000000000001111011101101,您将读到:

1
0
1
1
0
1
1
1
0
1
1
1
1
DONE

如果不创建缓存/哈希图存储{ [binaryNumber]: length }或任何类似的哈希表,你怎么能做到这一点呢?如何简单地从n函数的输入中导出它,即停止检查值的长度?

javascript
  • 1 个回答
  • 23 Views
Martin Hope
Lance
Asked: 2024-02-11 14:12:34 +0800 CST

如何让函数返回结果类型依赖于 TypeScript 中的输入参数?

  • 5

到目前为止我有这个TypeScript 游乐场:

export type Encoding = 'buffer' | 'blob' | 'base64'

export default async function md2docx<E extends Encoding>(
  text: string,
  encoding: E,
): Promise<
  E extends 'blob' ? Blob : E extends 'buffer' ? number : string
> {
  switch (encoding) {
    case 'buffer': return 10 //Buffer.from(text)
    case 'blob': return new Blob([text])
    case 'base64': atob(text)
  }
}

md2docx('foo', 'blob').then(output => {
  output.arrayBuffer
})

我基本上希望能够将编码类型传递给函数md2docx并让它返回正确的类型并在变量中正确输入它output。现在我的代码不起作用。如果我执行以下操作,那么我必须output instanceof Blob手动检查此类事情,这是我想避免的:

export type Encoding = 'buffer' | 'blob' | 'base64'

export default async function md2docx<E extends Encoding>(
  text: string,
  encoding: E,
) {
  switch (encoding) {
    case 'buffer': return 10 //Buffer.from(text)
    case 'blob': return new Blob([text])
    case 'base64': atob(text)
  }
}

md2docx('foo', 'blob').then(output => {
  if (output instanceof Blob) {
    output.arrayBuffer; // .arrayBuffer property exists.
  }
})
typescript
  • 1 个回答
  • 30 Views
Martin Hope
Lance
Asked: 2024-01-17 14:16:16 +0800 CST

如何修复 Zod 中“分配给‘string’类型参数的不安全类型‘any’参数?”?

  • 5

实际上,出于某种奇怪的原因,我在本地遇到一个错误(使用zod v3.22.4),而在使用 v3.22.4 的 stackblitz 上遇到另一个错误,所以不确定,也许这两个错误都需要修复。

stackblitz 示例与我本地的代码相同,是这样的:

import { z } from 'zod';

type BuildCommandToDecompressWithUnarchiver = {
  overwrite?: boolean;
  password?: string;
  output: {
    directory: {
      path: string;
    };
  };
  input: {
    file: {
      path: string;
    };
  };
};

const BuildCommandToDecompressWithUnarchiverModel: z.ZodType<BuildCommandToDecompressWithUnarchiver> =
  z.object({
    overwrite: z.optional(z.boolean()).default(false),
    password: z.optional(z.string()),
    output: z.object({
      directory: z.object({
        path: z.string(),
      }),
    }),
    input: z.object({
      file: z.object({
        path: z.string(),
      }),
    }),
  });

export function buildCommandToDecompressWithUnarchiver(source) {
  const input = BuildCommandToDecompressWithUnarchiverModel.parse(source);
  const cmd = [
    `unar`,
    `${input.input.file.path}`,
    `-o`,
    `${input.output.directory.path}`,
  ];

  cmd.push('--quiet');

  if (input.overwrite) {
    cmd.push(`-f`);
  }

  if (input.password) {
    cmd.push(`-p`, input.password);
  }

  return cmd;
}

console.log(
  BuildCommandToDecompressWithUnarchiverModel.parse({
    output: {
      directory: {
        path: 'foo',
      },
    },
    input: {
      file: {
        path: 'x.zip',
      },
    },
  })
);

首先,旁注。我需要z.ZodType<BuildCommandToDecompressWithUnarchiver>,因为在我的许多定义中,我使用嵌套模式,并且您必须经常使用此模式才能编译它。所以这需要留下来。

但我在 stackblitz 中得到的是:

Type 'ZodObject<{ overwrite: ZodDefault<ZodOptional<ZodBoolean>>; password: ZodOptional<ZodString>; output: ZodObject<{ directory: ZodObject<{ path: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>; }, "strip", ZodTypeAny, { ...; }, { ...; }>; input: ZodObject<...>; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<BuildCommandToDecompressWithUnarchiver, ZodTypeDef, BuildCommandToDecompressWithUnarchiver>'.
  The types of '_type.output.directory' are incompatible between these types.
    Type '{ path?: string; }' is not assignable to type '{ path: string; }'.
      Property 'path' is optional in type '{ path?: string; }' but required in type '{ path: string; }'.(2322)

我没有看到任何地方被定义path为可选,那么它是从哪里得到的呢?

但在本地,我看到了一个不同的错误:

Unsafe argument of type `any` assigned to a parameter of type `string`.eslint@typescript-eslint/no-unsafe-argument
(property) password?: string

在此输入图像描述

我的代码库中有十几个地方出现“any分配给类型参数的不安全参数类型”错误,所有地方都使用相同的模式来定义 zod 模式。string知道如何修复最后一个(以及可能为什么 stackblitz 不复制而是显示不同的错误,我有时也会在本地得到该错误)?

我解决最后一个错误的方法是只执行input.password as string,但这是不对的......此时已经保证输入了一个字符串。

typescript
  • 1 个回答
  • 16 Views
Martin Hope
Lance
Asked: 2024-01-07 16:14:29 +0800 CST

如何获取ImageMagick API中的输入和输出格式列表?

  • 6

我正在修改浏览器/wasm ImageMagick API,它有一个MagickFormat模块,基本上如下所示:

export declare enum MagickFormat {
    Unknown = "UNKNOWN",
    ThreeFr = "3FR",
    ThreeG2 = "3G2",
    ThreeGp = "3GP",
    A = "A",
    Aai = "AAI",
    Ai = "AI",
    Apng = "APNG",
    Art = "ART",
    Arw = "ARW",
    Ashlar = "ASHLAR",
    Avi = "AVI",
    Avif = "AVIF",
    Avs = "AVS",
    B = "B",
    Bayer = "BAYER",
    Bayera = "BAYERA",
    Bgr = "BGR",
    Bgra = "BGRA",
    Bgro = "BGRO",
    Bmp = "BMP",
    Bmp2 = "BMP2",
    Bmp3 = "BMP3",
    Brf = "BRF",
    C = "C",
    Cal = "CAL",
    Cals = "CALS",
    Canvas = "CANVAS",
    Caption = "CAPTION",
    Cin = "CIN",
    Cip = "CIP",
    Clip = "CLIP",
    Cmyk = "CMYK",
    Cmyka = "CMYKA",
    Cr2 = "CR2",
    Cr3 = "CR3",
    ...

本质上它列出了大约 260 种格式,但没有说明哪些格式可以作为输入,哪些格式可以作为输出。我尝试将 PNG 转换为 CR2,但 wasm API 失败,并显示:

Error: NoEncodeDelegateForThisImageFormat `CR2' @ error/constitute.c/WriteImage/1409

我知道这会失败,因为我在一些我不记得的随机网站上读到它,CR2是佳能相机原始格式,他们只能将其作为输入并使用它来生成更简单的输出,如JPG/PNG/等..但我的问题是,ImageMagick 允许的输入和输出格式到底是什么?我怎样才能弄清楚呢?有没有办法用 API 以某种方式列出它们(然后我可以将其转换为 wasm API)?理想情况下,我猜不是 CLI 示例,而是源代码 API 示例。

所有这些都被MagickFormat允许作为输入格式吗?并且只有一个子集作为输出格式?或者我在哪里可以找到哪些是允许的?哦,还有相关的,这是否取决于输入允许什么输出?(比如,它是“JPG 可以转换为 X 和 Y 但不能转换为 Z,但 PNG 可以转换为 X 和 Z 但不能转换为 Y”之类的矩阵,还是比这更简单?)我部分好奇在此过程中,如果您必须为每个输入类型创建一个到该输入的可能输出的一对多映射列表,或者比这更通用。

主要目标:弄清楚我可以进行哪些操作。

我也看到了该supportedFormats房产,但我不确定这是否完全解决了我的问题。

encoding
  • 1 个回答
  • 24 Views
Martin Hope
Lance
Asked: 2023-12-02 19:20:12 +0800 CST

GraphQL 查询的示例,其中单个记录类型有多个不同的过滤器类型,具体取决于它在查询树中的位置?

  • 4

我正在向 ChatGPT 寻求帮助,深入研究 GraphQL 查询的可能性,因为我正在尝试制作 GraphQL 的 JSON 变体以用于学习目的。我很困惑并且已经困惑了很长一段时间的一件事是如何支持在一个节点上查询输入参数/参数,但在另一个相同类型的节点上不支持查询输入参数/参数。

构建长的 ChatGPT 线程(您不需要阅读本文),假设您有一个像这样的系统:

const resolvers = {
  Query: {
    author: (parent, args) => {
      // Implement logic to fetch an author by ID
    },
    post: (parent, args) => {
      // Implement logic to fetch a post by ID
    },
  },
  Author: {
    posts: (author) => {
      // Implement logic to fetch all posts by this author
    },
  },
  Post: {
    comments: (post) => {
      // Implement logic to fetch all comments for this post
    },
  },
  Comment: {
    author: (comment) => {
      // Implement logic to fetch the author of this comment
    },
  },
};

我试图想象一个图像,其中有类似这样的查询:

query {
  author(id: "1") {
    id
    name
    posts(limit: 10) {
      id
      title
      content
      comments(limit: 10) {
        id
        text
        author {
          id
          name
          posts(createdAtGt: someDate) {
            id
            title
            content
          }
        }
      }
    }
  }
}

或者更复杂的东西。

基本上,我们在这里看到Query.author和Comment.author,所以问题的第一部分是,为什么需要作用域Comment.author,为什么不直接使用Query.author作用域?这不像我们正在做一个Author.posts.comments()作用域,而是有一个顶级Post.comments解析器。

但是,如果我们遇到这样一种情况(我还找不到一个简单清晰的例子),您想以一种方式过滤 的帖子author.posts,但以完全不同的方式过滤comment.author.posts, _并且您不想允许过滤一个上下文中的帖子将用于查询树中另一上下文中的 GraphQL 查询...

你有遇到过这样的情况吗?

基本上我想知道为什么你会选择一个方向而不是另一个方向:

  1. 要么只允许一种类型的过滤在特定类型的所有节点/记录上起作用。
  2. 或者 2,在每个分支级别指定唯一的输入/过滤功能,因此一种类型的记录可能根据其在查询树中的位置具有 10 种不同的过滤机制。

如果(2)发生在更复杂的生产应用程序中,那么您似乎必须在解析器的许多不同位置重新实现大量基本逻辑,然后在需要的地方进行自定义。基本上想知道为什么你不直接构建一个看起来更像这样的解析器(记住我正在集体讨论 GraphQL 的 JSON 替代方案以用于学习目的):

{
  author: {
    resolve(parent, args) {
      // Find author by id
    },
    children: {
      posts: {
        resolve(parent) {
          // find posts by author id, and allow `limit` on the posts query, only here.
        },
        children: {
          comments: {
            resolve(parent) {
              // find comments by post id
            },
            children: {
              author: {
                resolve(parent) {
                  // find author by comment author id
                },
                children: {
                  posts: {
                    resolve(parent) {
                      // find posts by author id, but only allow searching for greater than certain date.
                    }
                  }
                }
              }
            }
          }
        }
      },
      comments: {
        resolve(parent) {
          // now find comments by author id
        },
        children: {
          author: {
            resolve(parent) {
              // maybe here we have different logic and only return authors with a first name starting with "A".
            },
            children: {
              posts: {
                resolve(parent) {
                  // and another variant here too... it can go on more and more custom cases.
                }
              }
            }
          }
        }
      }
    }
  }
}

GraphQL 领域是否发生过类似的事情?如果是这样,要演示的半复杂示例是什么?如果没有,为什么不呢?

graphql
  • 1 个回答
  • 27 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve