Last active
May 21, 2019 22:52
-
-
Save nedzadarek/a72f06a3a0249db1dafffecf72735976 to your computer and use it in GitHub Desktop.
Trying to implement each from the K language: https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.md#adverb-reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Opinions:
merge
) in different cases (append
vsinsert
). Without function likemerge
it can be simulated by 2nd function (eachleft instead of eachright, or eachright instead of eachleft):Still... I don't remember using such feature. As with 3) examples needed.