libera/#commonlisp - IRC Chatlog
Search
21:52:37
phoe
I have a list of numbers and I want to get a list of their sub-sums, so e.g. (1 2 3 4 5 ...) -> (1 3 6 10 15 ...)
21:54:25
moon-child
that's a prefix scan, which is a variant of reduce. I don't know of a specific implementation strategy, but you can use that keyword to search for one
21:55:43
jcowan
the nice thing about reduce over fold is that if you know the procedure is associative you can use a parallel algorithm.
22:01:16
edgar-rft
(let ((sum 0)) (mapcar (lambda (x) (incf sum x)) (list 1 2 3 4 5))) => (1 3 6 10 15)
22:04:11
edgar-rft
but code that I still can understand at the first read in hundred years from now...
22:48:30
jcowan
moon-child: No. The main difference is that you fold using a plain function and you reduce using a monoid.
22:49:48
jcowan
In a monoid, you always statically know the start value, instead of it being passed to the fold: thus the normal + monoid returns 0 if applied to zero arguments. Third, fold can be done in parallel because a monoid is guaranteed to be associative.
1:20:03
pjb
phoe: (mapcar (let ((s 0)) (lambda (i) (incf s i))) (iota 10)) #| --> (0 1 3 6 10 15 21 28 36 45) |#
1:26:29
moon-child
(actually, I would have expected the former to perform better, but sbcl seems to generate the same code for both)
1:32:14
pjb
If you look at the code of the minimal-compiler, (or just compiler), it would be obvious why the same is generated.
5:16:58
kami_
I'm trying to 'translate' this code: https://github.com/metacall/core/blob/develop/source/tests/metacall_node_test/source/metacall_node_test.cpp#L78
5:19:52
kami_
I'm creating a server where people have the option to provide their own functions as hooks into the standard processing of some resources
5:33:53
moon-child
kami_: I expect you need to define the allocator_std_type structure before you can use it
5:35:47
kami_
moon-child: thanks. I'm bad at communicating things properly. The headers were grovelled with c2ffi, I'll paste the definition