标签: lazy-sequences

流处理中欧拉的高效筛法

Sieve of eulerSieve of Eratosthenes具有更好的渐近复杂度,并且可以简单地用命令式语言实现。

我想知道是否有任何方法可以用流优雅地实现它。我已经检查了关于素数haskell wiki,但是这两个实现比该wiki中的其他筛子(甚至试验分区!)慢数百倍。

所以我尝试自己写:

{-sieve of Euler-}
primesEU = 2:sieve [3,5 ..] where
    sieve (p:i:xt) = p:(sieve (i:xt) `minus` (lps i)) where
        lps i = map (i*) (takeWhile' (\p->(p<i)&&(mod i p /= 0)) primesEU)

takeWhile'::(t->Bool)->[t]->[t]
takeWhile' p [] = []
takeWhile' p (x:xs) = case (p x) of
    True  -> x:takeWhile' p xs
    False -> [x]

minus::(Ord t) => [t]->[t]->[t]
minus [] _ = []
minus xs [] = …
Run Code Online (Sandbox Code Playgroud)

primes haskell sieve lazy-sequences

2
推荐指数
1
解决办法
530
查看次数

OCaml 中 Python 枚举函数最简单的模拟是什么?

在 Python 中枚举的工作原理如下:

a_list = ['a', 'b', 'c']
    
for i, x in enumerate(a_list): 
    print(i, x)
Run Code Online (Sandbox Code Playgroud)

输出将是:

0 a
1 b
2 c
Run Code Online (Sandbox Code Playgroud)

因此, enumerate 实际上返回一个 (i, x) 形式对的生成器(几乎是一个惰性序列),其中 i 的范围超过 0, 1, 2, ...,x 是列表中按顺序排列的元素。

到目前为止,我已经提出了列表的定义,它不会产生“生成器”,但也会产生一个对的列表:


let enumerate (a_list: 'a list): (int * 'a) list =
  let rec _enumerar (a_list: 'a list) (accum: (int * 'a) list) (i: int): (int * 'a) list =
    match a_list with
    | [] -> accum
    | x::xs -> _enumerar  xs ((i, x)::accum) (i+1)
  in …
Run Code Online (Sandbox Code Playgroud)

python ocaml list enumerate lazy-sequences

2
推荐指数
1
解决办法
433
查看次数

clojure:如何从懒惰的seq中获取值?

我是clojure的新手,需要一些帮助来从懒惰序列中获取值.

您可以在这里查看我的完整数据结构:http://pastebin.com/ynLJaLaP 我需要的是标题的内容:

{: _content AlbumTitel2}
Run Code Online (Sandbox Code Playgroud)

我设法得到所有_content值的列表:

(def albumtitle (map #(str (get % :title)) photosets))
(println albumtitle)
Run Code Online (Sandbox Code Playgroud)

结果是:

({:_content AlbumTitel2} {:_content test} {:_content AlbumTitel} {:_content album123}   {:_content speciale} {:_content neues B5 Album} {:_content Album Nr 2})
Run Code Online (Sandbox Code Playgroud)

但是我怎样才能获得每一个的价值:_content?

任何帮助,将不胜感激!

谢谢!

clojure lazy-sequences

1
推荐指数
1
解决办法
970
查看次数

在clojure中使用惰性序列

我想知道lazy-seq返回有限列表或无限列表.有一个例子,

(defn integers [n]
    (cons n (lazy-seq (integers (inc n)))))
Run Code Online (Sandbox Code Playgroud)

当我跑的时候

(first integers 10)
Run Code Online (Sandbox Code Playgroud)

要么

(take 5 (integers 10))
Run Code Online (Sandbox Code Playgroud)

结果是10和(10 11 12 13 14).但是,当我跑

(integers 10)
Run Code Online (Sandbox Code Playgroud)

该过程无法打印任何内容,无法继续.有没有人可以告诉我为什么和laza-seq的用法.非常感谢!

clojure lazy-sequences

1
推荐指数
1
解决办法
240
查看次数

试图了解Kotlin示例

我想学习Kotlin并正在研究try.kotlinlang.org上的示例

我听不太懂一些例子,特别是懒惰的属性例如:https://try.kotlinlang.org/#/Examples/Delegated%20properties/Lazy%20property/Lazy%20property.kt

/**
 * Delegates.lazy() is a function that returns a delegate that implements a lazy property:
 * the first call to get() executes the lambda expression passed to lazy() as an argument
 * and remembers the result, subsequent calls to get() simply return the remembered result.
 * If you want thread safety, use blockingLazy() instead: it guarantees that the values will
 * be computed only in one thread, and that all threads will see the same value.
 */ …
Run Code Online (Sandbox Code Playgroud)

lambda delegates lazy-sequences kotlin

1
推荐指数
1
解决办法
236
查看次数

Trying to split string in Clojure running into lazy seq problem

I am working on a problem to read in a file with lines like:

A abcdefg
B bcdefgh
Run Code Online (Sandbox Code Playgroud)

But I keep getting errors about Lazy Sequence not compatible with Java Charseq ..

I tried:

(def notlazy (doall lyne2))
Run Code Online (Sandbox Code Playgroud)

Then thought I verified:

(realized? notlazy)
true
Run Code Online (Sandbox Code Playgroud)

But still:

(str/split notlazy #" ")
ClassCastException class clojure.lang.LazySeq cannot be cast to class
  java.lang.CharSequence (clojure.lang.LazySeq is in unnamed module of
  loader 'app'; java.lang.CharSequence is in module java.base of loader
  'bootstrap')  clojure.string/split (string.clj:219)
Run Code Online (Sandbox Code Playgroud)

Help please!

clojure lazy-evaluation lazy-sequences

1
推荐指数
1
解决办法
141
查看次数

生成素数时“应用程序:不是程序”

我正在尝试输出前 100 个素数并不断收到错误消息:

应用程序:不是程序;期望一个可以应用于给定参数的过程:(#) arguments...:[none]

错误显示在我的 take$ 程序中:

(if (or (= m 0) (null? st))
      '()
      (cons (car st) (take$ (- m 1) ((cdr st)))))))
Run Code Online (Sandbox Code Playgroud)

这是我所有的代码:

(define int-builder$
    (lambda (x)
       (list x (lambda () (int-builder$ (+ 1 x ))))))

(define take$
    (lambda (m st)
       (if (or (= m 0) (null? st))
           '()
           (cons (car st) (take$ (- m 1) ((cdr st)))))))

(define filter-out-mults$
   (lambda (num  st)
     (cond
     (( = (remainder (car st) num) 0) 
         (filter-out-mults$ num ((cadr st))))
         (else 
            (list …
Run Code Online (Sandbox Code Playgroud)

scheme primes sieve lazy-sequences non-procedure-application

0
推荐指数
1
解决办法
487
查看次数

Scala错误:当代码出现在函数中时,"前向引用扩展了值的定义"

我正在尝试使用Scala 2.11.7编译以下代码.

object LucasSeq {
  val fibo: Stream[Int] = 0 #:: 1 #:: fibo.zip(fibo.tail).map { pair =>
    pair._1 + pair._2
  }

  def firstKind(p: Int, q: Int): Stream[Int] = {
    val lucas: Stream[Int] = 0 #:: 1 #:: lucas.zip(lucas.tail).map { pair =>
      p * pair._2 - q * pair._1
    }
    lucas
  }
}
Run Code Online (Sandbox Code Playgroud)

fibo基于Scala Stream文档中Fibonacci序列示例,它可以工作.

但是,firstKind试图用参数推广序列pq(制作第一类Lucas序列)的函数有以下错误:

LucasSeq.scala:7: error: forward reference extends over definition of value lucas
    val …
Run Code Online (Sandbox Code Playgroud)

scala compiler-errors forward-reference lazy-sequences

0
推荐指数
1
解决办法
964
查看次数

为什么时间宏声称慢函数调用的运行时间非常短?

正在看clojure第 9 章底部的练习,寻找勇敢和真实的人 (特别是搜索多个引擎并返回每个引擎的第一次命中的最后一个)

我用 slurp 部分嘲笑实际搜索是这样的:

(defn search-for
  [query engine]
  (Thread/sleep 2000)
  (format "https://www.%s.com/search?q%%3D%s", engine query))
Run Code Online (Sandbox Code Playgroud)

并实现了这样的行为:

(defn get-first-hit-from-each
  [query engines]
  (let [futs (map (fn [engine]
                    (future (search-for query engine))) engines)]
    (doall futs)
    (map deref futs)))
Run Code Online (Sandbox Code Playgroud)

(我知道这里的返回是一个列表,练习要求一个向量,但可以into为此做一个......)

但是当我在 REPL 中运行它时

(time (get-first-hit-from-each "gray+cat" '("google" "bing")))
Run Code Online (Sandbox Code Playgroud)

添加后似乎需要 2 秒doall(因为 map 返回一个惰性 seq,我认为除非我使用 seq,否则任何期货都不会启动,(last futs)似乎也有效)但是当我time在 REPL 中使用宏时,它会报告即使需要 2 秒,也几乎不消耗时间:

(time (get-first-hit-from-each "gray+cat" '("google" "bing")))
"Elapsed time: 0.189609 msecs"
("https://www.google.com/search?q%3Dgray+cat" "https://www.bing.com/search?q%3Dgray+cat") …
Run Code Online (Sandbox Code Playgroud)

future clojure lazy-evaluation lazy-sequences

0
推荐指数
1
解决办法
95
查看次数

如何计算一个非常大的惰性序列中的元素数量?

我有以下(非常大)lazy-seq

(def lazy-list (partition-all 100000 (take 10000000000000000000 (repeat 1))))
Run Code Online (Sandbox Code Playgroud)

我想计算其中的元素数量,为此我正在执行以下操作:

(time
  (loop [ll lazy-list
         c 0]
    (if-not (seq (take 1 ll))
      c
      (recur (drop 1 ll)
             (inc c)))))
Run Code Online (Sandbox Code Playgroud)

如果我运行这个,我会收到以下错误:

Execution error (OutOfMemoryError) at user/eval2043$fn (REPL:1).
Java heap space
Run Code Online (Sandbox Code Playgroud)

但如果我没有持有head任何地方,为什么我会看到这个OOM问题?

memory clojure out-of-memory large-data lazy-sequences

0
推荐指数
1
解决办法
140
查看次数