Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

See here for more information about sequences and generators. This nice application This nice application made me actually understand how you can create your own sequences.

See here for more information about sequences and generators. This nice application made me actually understand how you can create your own sequences.

See here for more information about sequences and generators. This nice application made me actually understand how you can create your own sequences.

Swift 2 update.
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95

Update: Due to major changes in the Swift programming language, the above code does not compile anymore with the current Swift 2.1/Xcode 7. Here is an updated version fore anybody's convenience:

struct FibonacciSequence : SequenceType {
 let upperBound : Int
 
 func generate() -> AnyGenerator<Int> {
 var current = 1
 var next = 1
 return anyGenerator {
 if current > self.upperBound {
 return nil
 }
 let result = current
 current = next
 next += result
 return result
 };
 }
}
let fibseq = FibonacciSequence(upperBound: 4_000_000).lazy
let sum = fibseq.filter { 0ドル % 2 == 0 }.reduce(0) { 0ドル + 1ドル }

Update: Due to major changes in the Swift programming language, the above code does not compile anymore with the current Swift 2.1/Xcode 7. Here is an updated version fore anybody's convenience:

struct FibonacciSequence : SequenceType {
 let upperBound : Int
 
 func generate() -> AnyGenerator<Int> {
 var current = 1
 var next = 1
 return anyGenerator {
 if current > self.upperBound {
 return nil
 }
 let result = current
 current = next
 next += result
 return result
 };
 }
}
let fibseq = FibonacciSequence(upperBound: 4_000_000).lazy
let sum = fibseq.filter { 0ドル % 2 == 0 }.reduce(0) { 0ドル + 1ドル }
Minor simplification, better comments.
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95

The following is similar to Flambino's approach, but creates a Swift SequenceType so that you can use the Swift library functions filter() and reduce() to iterate over the elements:

struct FibonacciSequence : SequenceType {
 let upperBound : Int
 
 func generate() -> GeneratorOf<Int> {
 var current = 1
 var next = 1
 return GeneratorOf<Int>() {
 if current > self.upperBound {
 return nil
 }
 let result = current
 current = next
 next += result
 return result
 };
 }
}
let fibseq = lazy(FibonacciSequence(upperBound: 4_000_000))
let evenFibseqsum = LazySequencereduce(fibseq).filter { 0ドル % 2 == 0 }
let sum = reduce(evenFibseq, 0) { 0ドル + 1ドル }

See here for more information about sequences and generators. This nice application made me actually understand how you can create your own sequences.

lazy() creates a LazySequence is used here because its, whose filter() method returns the elements elements "on demand", e.g. as needed in the summation. The filter() function would return an array of all even Fibonacci numbers before starting the summation. (Kudos to @jtbandes for for his suggestion).

The following is similar to Flambino's approach, but creates a Swift SequenceType so that you can use the Swift library functions filter() and reduce() to iterate over the elements:

struct FibonacciSequence : SequenceType {
 let upperBound : Int
 
 func generate() -> GeneratorOf<Int> {
 var current = 1
 var next = 1
 return GeneratorOf<Int>() {
 if current > self.upperBound {
 return nil
 }
 let result = current
 current = next
 next += result
 return result
 };
 }
}
let fibseq = FibonacciSequence(upperBound: 4_000_000)
let evenFibseq = LazySequence(fibseq).filter { 0ドル % 2 == 0 }
let sum = reduce(evenFibseq, 0) { 0ドル + 1ドル }

See here for more information about sequences and generators. This nice application made me actually understand how you can create your own sequences.

LazySequence is used here because its filter() method returns the elements "on demand", e.g. as needed in the summation (Kudos to @jtbandes for his suggestion).

The following is similar to Flambino's approach, but creates a Swift SequenceType so that you can use the Swift library functions filter() and reduce() to iterate over the elements:

struct FibonacciSequence : SequenceType {
 let upperBound : Int
 
 func generate() -> GeneratorOf<Int> {
 var current = 1
 var next = 1
 return GeneratorOf<Int>() {
 if current > self.upperBound {
 return nil
 }
 let result = current
 current = next
 next += result
 return result
 };
 }
}
let fibseq = lazy(FibonacciSequence(upperBound: 4_000_000))
let sum = reduce(fibseq.filter { 0ドル % 2 == 0 }, 0) { 0ドル + 1ドル }

See here for more information about sequences and generators. This nice application made me actually understand how you can create your own sequences.

lazy() creates a LazySequence, whose filter() method returns the elements "on demand", e.g. as needed in the summation. The filter() function would return an array of all even Fibonacci numbers before starting the summation. (Kudos to @jtbandes for his suggestion).

Improved using lazy sequences as suggested by jtbandes.
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
added 5 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
added 84 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
added 483 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
fixed embarrassing error :)
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
URL's are ugly :)
Source Link
syb0rg
  • 21.9k
  • 10
  • 113
  • 192
Loading
added 206 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
default

AltStyle によって変換されたページ (->オリジナル) /