Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Forward-difference/E/forward-difference.e
Normal file
32
Task/Forward-difference/E/forward-difference.e
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
pragma.enable("accumulator")
|
||||
/** Single step. */
|
||||
def forwardDifference(seq :List) {
|
||||
return accum [] for i in 0..(seq.size() - 2) {
|
||||
_.with(seq[i + 1] - seq[i])
|
||||
}
|
||||
}
|
||||
|
||||
/** Iterative implementation of the goal. */
|
||||
def nthForwardDifference1(var seq :List, n :(int >= 0)) {
|
||||
for _ in 1..n { seq := forwardDifference(seq) }
|
||||
return seq
|
||||
}
|
||||
|
||||
/** Imperative implementation of the goal. */
|
||||
def nthForwardDifference2(seq :List, n :(int >= 0)) {
|
||||
def buf := seq.diverge()
|
||||
def finalSize := seq.size() - n
|
||||
for lim in (finalSize..!seq.size()).descending() {
|
||||
for i in 0..!lim {
|
||||
buf[i] := buf[i + 1] - buf[i]
|
||||
}
|
||||
}
|
||||
return buf.run(0, finalSize)
|
||||
}
|
||||
|
||||
? def sampleData := [90, 47, 58, 29, 22, 32, 55, 5, 55, 73]
|
||||
> for n in 0..10 {
|
||||
> def r1 := nthForwardDifference1(sampleData, n)
|
||||
> require(r1 == nthForwardDifference2(sampleData, n))
|
||||
> println(r1)
|
||||
> }
|
||||
Loading…
Add table
Add a link
Reference in a new issue