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.
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ドル }
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).