Skip to content

Instantly share code, notes, and snippets.

@nedzadarek
Last active May 21, 2019 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nedzadarek/a72f06a3a0249db1dafffecf72735976 to your computer and use it in GitHub Desktop.
Save nedzadarek/a72f06a3a0249db1dafffecf72735976 to your computer and use it in GitHub Desktop.
Red [
author: "Nędza Darek"
license: "Just link to this gist"
]
gitter-source: https://gitter.im/red/help?at=5ce3990b9d64e537bce51706
k-manual: https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.md
; The primitive verbs in K are either monadic (have only one argument - right, or x) or
; dyadic (have two arguments - left and right, or x and y).
; m is monadic, d is dyadic
; 1) m'l is each. Apply the monad to each x, producing a new list
add1: function [a] [a + 1]
each1: function [series [series!] fun[function!]] [
collect [
foreach el series [
keep do [fun el]
]
]
]
arr: [1 2 3]
each1 arr :add1
; == [2 3 4]
; 2) x d'y is each dyad. Pair up values from x and y and apply them to the dyad, producing a new list
add2: function [a b] [a + b]
each2: function [series1 [series!] series2 [series!] fun[function!]] [
either (length? series1) = (length? series2) [
collect [
while [not tail? series1] [
keep do [fun series1/1 series2/1]
series1: next series1
series2: next series2
]
]
] [
do make error! "Not equal length"
]
]
arr1: [1 2 3]
arr2: [10 20 30]
each2 arr1 arr2 :add2
; == [11 22 33]
; 3) d':l is eachprior. Apply the dyad to each element of the list (left argument) and
; the element preceding that element in the list (right argument), producing a new list.
add3: function [a b] [a + b]
each3: function [series [series!] fun[function!] zero-value] [
collect [
keep do [fun series/1 zero-value]
a-series: next series
b-series: series
while [not tail? a-series] [
keep do [fun a-series/1 b-series/1]
a-series: next a-series
b-series: next b-series
]
]
]
arr1: [1 2 3]
each3 arr1 :add3 0
; == [1 3 5]
each3 arr1 :add3 10
; == [11 3 5]
; 4) x d/:l is eachright. Apply the dyad to the entire left argument and each right argument, producing a new list.
each4: function [value series [series!] fun[any-function!]] [
collect [
foreach element series [
keep/only do[fun value element]
]
]
]
merge: function [a b] [
; [1 2 3] 4
; [1 2 3] [4 5 6]
either block? a [
append copy a b
][
; 1 [2 3 4]
head insert copy b a
]
]
; merge [1 2 3] 4
; ; == [1 2 3 4]
; merge [1 2 3] [4 5 6]
; ; == [1 2 3 4 5 6]
; merge 1 [2 3 4]
; ; == [1 2 3 4]
arr1: [1 2]
arr2: [10 20 30]
each4 arr1 arr2 :merge
; == [[1 2 10] [1 2 20] [1 2 30]]
;l d\:x is eachleft. Apply the dyad to each left argument and the entire right argument, producing a new list.
; REST IN COMMENTS
each5: function [ series [series!] value fun[any-function!]] [
collect [
foreach element series [
keep/only do[fun element value]
]
]
]
arr1: [1 2]
arr2: [10 20 30]
each5 arr1 arr2 :merge
; == [[1 10 20 30] [2 10 20 30]]
@nedzadarek
Copy link
Author

Opinions:

  1. Monadic each: It's very useful. I have seen it in the almost all "not basic" language.
  2. Diadic each: From time to time I need to do things with 2 series. It would be useful to have it in a language but is it necessary? Do somebody use it? Does somebody have examples?
  3. Eachprior: I have no idea where I can use it. Does somebody has examples?
  4. & 5) They are very similar. The power of it comes from a function used. We can use one function (in my case merge) in different cases (append vs insert). Without function like merge it can be simulated by 2nd function (eachleft instead of eachright, or eachright instead of eachleft):
append*: func [v s] [append copy s v]
each5 arr2 arr1 :append*
; == [[1 2 10] [1 2 20] [1 2 30]]
each4 arr1 arr2 :merge
; == [[1 2 10] [1 2 20] [1 2 30]]

Still... I don't remember using such feature. As with 3) examples needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment