Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #54071
| From | "Robert L." <No_spamming@noWhere_7073.org> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Rosetta Code: Accumulator factory |
| Date | 2017-01-17 09:00 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <o5kmev$tfm$1@gioia.aioe.org> (permalink) |
A problem posed by Paul Graham is that of creating a function
that takes a single (numeric) argument and which returns
another function that is an accumulator. The returned
accumulator function in turn also takes a single numeric
argument, and returns the sum of all the numeric values passed
in so far to that accumulator (including the initial value
passed when the accumulator was created).
Takes a number n and returns a function (lets call it g), that
takes a number i, and returns n incremented by the
accumulation of i from every call of function g(i).
Works for any numeric type-- i.e. can take both ints and
floats and returns functions that can take both ints and
floats. (It is not enough simply to convert all input to
floats. An accumulator that has only seen integers must return
integers.)
Generates functions that return the sum of every number ever
passed to them, not just the most recent. (This requires a
piece of state to hold the accumulated value, which in turn
means that pure functional languages can't be used for this
task.)
Ruby:
def factory(n)
proc{|i| n += i}
end
f20 = factory(20)
f500 = factory(500)
p f500.call(3) ; p f500[3.0] ; p f500.(3)
===>
503
506.0
509.0
p f20[2] ; p f20[2]
===>
22
24
In Forth?
--
[A] young British journalism student ... ventured to the 'refugee' camp at
Calais to report sympathetically on their 'plight.' She was given a crash
course in race realism when the poor downtrodden victims of Western oppression
subjected her to a gang-rape described by police as being of "a particularly
brutal nature."
theoccidentalobserver.net/2015/08/thoughts-on-the-german-dispossession/
Back to comp.lang.forth | Previous | Next — Next in thread | Find similar | Unroll thread
Rosetta Code: Accumulator factory "Robert L." <No_spamming@noWhere_7073.org> - 2017-01-17 09:00 +0000
Re: Rosetta Code: Accumulator factory Paul Rubin <no.email@nospam.invalid> - 2017-01-17 01:38 -0800
Re: Rosetta Code: Accumulator factory anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-01-17 13:54 +0000
Re: Rosetta Code: Accumulator factory Paul Rubin <no.email@nospam.invalid> - 2017-01-17 13:10 -0800
Re: Rosetta Code: Accumulator factory Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-01-18 03:15 -0600
Re: Rosetta Code: Accumulator factory anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-01-18 13:43 +0000
Re: Rosetta Code: Accumulator factory Paul Rubin <no.email@nospam.invalid> - 2017-01-18 13:20 -0800
Re: Rosetta Code: Accumulator factory albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-01-19 02:40 +0100
Re: Rosetta Code: Accumulator factory anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-01-19 16:27 +0000
csiph-web