RosettaCodeData/Task/Currying/M2000-Interpreter/currying.m2000

60 lines
1.8 KiB
Plaintext

Module LikeGroovy {
divide=lambda (x, y)->x/y
Curry=lambda (f as lambda, k)->(lambda f, k ->(f(k,![])))
partsof120=Curry(divide, 120)
Print "half of 120 is ";partsof120(2)
Print "a third is ";partsof120(3)
Print "and a quarter is ";partsof120(4)
joinTwoWordsWithSymbol=lambda (s, a, b)->a+s+b
Assert joinTwoWordsWithSymbol("#","Hello", "World")="Hello#World"
concatWords =Curry(joinTwoWordsWithSymbol, " ")
Assert concatWords("Hello", "World")="Hello World"
prependHello =Curry(concatWords, "Hello")
Assert prependHello("World")="Hello World"
Print "done"
}
LikeGroovy
Module M2000way {
class curry{
private:
p=stack ' stack of values
func$ ' field for the weak reference
public:
property counter {value } ' readonly
class:
module curry(.func$) {
.p<=[]
class value {
value () {
' symbol ! used for feeding stack of values from arrays or stacks.
' [] is the current stack (leave emtpy stack as current stack)
' Stack(.p) make a copy of .p (which have a stack object)
=function(.func$, !stack(.p), ![])
.[counter]++
}
}
this=value() ' make this as a property
}
}
' using a general function
Function Divide(a, b) {
=a/b
}
Print "Divide(10, 6) is ";Divide(10, 5)
partsof120=Curry(&Divide(), 120)
Print "half of 120 is ";partsof120(2)
Print "a third is ";partsof120(3)
Print "and a quarter is ";partsof120(4)
Print "Use of partsof120 so far: "; partsof120.counter; " times"
' using a lambda function
joinTwoWordsWithSymbol=lambda (s, a, b)->a+s+b
Assert joinTwoWordsWithSymbol("#","Hello", "World")="Hello#World"
concatWords =Curry(&joinTwoWordsWithSymbol, " ")
Assert concatWords("Hello", "World")="Hello World"
prependHello =Curry(&concatWords, "Hello")
Assert prependHello("World")="Hello World"
Print "done"
}
M2000way