lazy-evaluation

Laziness in Swift

大憨熊 提交于 2019-12-04 08:19:18
问题 Why is lazy used here? extension SequenceType { func mapSome<U>(transform: Generator.Element -> U?) -> [U] { var result: [U] = [] for case let x? in lazy(self).map(transform) { result.append(x) } return result } } this extension takes a transformation function that returns an optional, and returns an array of only those values that weren’t transformed into nil Why not just use self.map(transform) ? is laziness necessary here? 回答1: It avoids the creation of an intermediate array. self.map

When is my Haskell expression evaluated?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 07:59:42
If I define λ> data Bar = Bar Int deriving Show λ> data Foo = Foo Bar deriving Show and λ> let foo = trace "foo" Foo (trace "bar" Bar 100) λ> let two = trace "two" 2 λ> let g (Foo x) y = y then I think I understand why I get λ> g foo two foo two 2 But if I then repeat this, I get λ> g foo two two 2 and I don't understand why foo appears not to have been evaluated for the second invocation of g , especially since it is clearly not (yet) somehow already available, as I can verify with λ> foo Foo bar (Bar 100) though — again, to my confusion — repeating the previous gives λ> foo Foo (Bar 100) Why

How many ways are there to describe the Fibonacci sequence in Perl 6?

眉间皱痕 提交于 2019-12-04 07:52:29
问题 I've been looking at the various ways of constructing lazy lists in Perl 6 and I would like to collect all of the concise ways of describing the Fibonacci sequence. I will start this off with the three from masak's journal: my @fibs := (0, 1, -> $a, $b { $a + $b } ... *); my @fibs := (0, 1, { $^a + $^b } ... *); my @fibs := (0, 1, *+* ... *); I was thinking something like this would also work, but I think I have the syntax wrong: my @fibs := (0, 1, (@fibs Z+ @fibs[1..*])); Something there is

lazy attribute in Swift equivalent to lazy Init getter in Objective C

前提是你 提交于 2019-12-04 07:37:13
Is the lazy attribute in Swift equivalent to overriding the getter with a lazy loading pattern in Objective C? Oscar Swanros From the docs: A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy attribute before its declaration. So, mostly, yes. You must always declare a lazy property as a variable (with the var keyword), because its initial value may not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization

Lazy Quicksort in Scala

杀马特。学长 韩版系。学妹 提交于 2019-12-04 07:28:24
Is it possible to do this sort of thing in Scala? def quicksort[A](xs: Stream[A])(implicit o: Ordering[A]): Stream[A] = { import o._ if (xs.isEmpty) xs else { val (smaller, bigger) = xs.tail.partition(_ < xs.head) quicksort(smaller) #::: xs.head #:: quicksort(bigger) } } It can be done with views as well, though it's bound to be much slower: def quicksort[A](xs: List[A])(implicit o: Ordering[A]) = { import o._ def qs(xs: SeqView[A, List[A]]): SeqView[A, Seq[_]] = if (xs.isEmpty) xs else { val (smaller, bigger) = xs.tail.partition(_ < xs.head) qs(smaller) ++ (xs.head +: qs(bigger)) } qs(xs.view

Use of lazy val for caching string representation

半城伤御伤魂 提交于 2019-12-04 06:14:29
I encountered the following code in JAXMag's Scala special issue: package com.weiglewilczek.gameoflife case class Cell(x: Int, y: Int) { override def toString = position private lazy val position = "(%s, %s)".format(x, y) } Does the use of lazy val in the above code provide considerably more performance than the following code? package com.weiglewilczek.gameoflife case class Cell(x: Int, y: Int) { override def toString = "(%s, %s)".format(x, y) } Or is it just a case of unnecessary optimization? One thing to note about lazy vals is that, while they are only calculated once, every access to

Lazily concatenate an enumerable of lists

霸气de小男生 提交于 2019-12-04 05:41:14
I would like to write a function akin to List.concat/1 that takes an enumerable of lists and emits the concatenated lists as a continuous stream. It would work like this: iex> 1..3 |> Stream.map(&([&1])) |> Enum.to_list [[1], [2], [3]] iex> 1..3 |> Stream.map(&([&1])) |> MyStream.concat |> Enum.to_list [1, 2, 3] What I have come up with so far is this: defmodule MyStream do def concat(lists) do Enumerable.reduce(lists, [], fn(x, acc) -> acc ++ x end) end end This produces the correct result but obviously isn't lazy. I have unsuccessfully tried using Stream.Lazy but really fail to understand

android lazy loading not showing images on phone or showing and is slow

…衆ロ難τιáo~ 提交于 2019-12-04 05:32:50
问题 I am using JSON to parse an online xml document and also 2 methods for lazy image loading. Below is my source code, explanation and my problem: Explanation: Method 1: Use AsyncTask and line imageLoader.DisplayImage((String)jsonImageText.get("imageLink"), activity, imageView); inside the adapter. Method 2: Not use AsyncTask and use the rest of the try-catch block and NOT the line imageLoader.DisplayImage((String)jsonImageText.get("imageLink"), activity, imageView); The problems: Method 1: The

How to add a classmethod in Python dynamically

心不动则不痛 提交于 2019-12-04 05:19:49
I'm using Python 3. I know about the @classmethod decorator. Also, I know that classmethods can be called from instances. class HappyClass(object): @classmethod def say_hello(): print('hello') HappyClass.say_hello() # hello HappyClass().say_hello() # hello However, I don't seem to be able to create class methods dynamically AND let them be called from instances. Let's say I want something like class SadClass(object): def __init__(self, *args, **kwargs): # create a class method say_dynamic SadClass.say_dynamic() # prints "dynamic!" SadClass().say_dynamic() # prints "dynamic!" I've played with

In python, can I lazily generate copies of an iterator using tee?

孤街醉人 提交于 2019-12-04 04:48:06
问题 I'm trying to create an iterator which lazily creates (potentially infinitely many) copies of an iterator. Is this possible? I know I can create any fixed finite number of copies by simply doing iter_copies = tee(my_iter, n=10) but this breaks down if you don't know n ahead of time or if n is infinite. I would usually try something along the lines of def inf_tee(my_iter): while True: yield tee(my_iter)[1] But the documentation states that after using tee on an iterator the original iterator