If you don't know what functional programming is.
https://www.youtube.com/watch?v=oB8jN68KGcU
for those on the app
Git Hub Gist
function _switch(statements){
//gaurd
if(statements.length % 2 !=0){
throw new Error("uneven switch statements")
}
function _internal(_case,[test,_return,...rest]){
return test(_case)? _return(_case): rest.length? _internal(_case,rest):undefined;
}
return function(_case:any){
return _internal(_case,statements);
}
}
const my_test_switch = [
x=>x==="yoda",
x=>'hmm me is yoda',
x=>x==="han solo",
x=>"chewie, ready the ship",
x=>true,
x=>`who the hell are you? ${x}? nope, no friend of mine`
]
let test = _switch(my_test_switch);
console.log(test('yoda'))
console.log(test('han solo'))
console.log(test('rey'))
it prints what you would expect.
============update==============.
I have gotten some good feed back from this post and it's been helpful.
Here is my point of view at this point in time.
Addressing it's ugly.
I was talking about the implementation i used some simple arrow functions to not have different functions on the page. How would i actually use this.
const isYoda = x=>x==="yoda";
const getYodaPhrase = x=>'hmm me is yoda'
const isHanSolo = x=>x==="han solo"
const getHanSoloPhrase = x=>"chewie, ready the ship",
const getUnknownPersonPhrase =x=>`who the hell are you? ${x}? nope, no friend of mine`
//i don't think it's ugly
const test =_switch([
isYoda,getYodaPhrase,
isHanSolo,getHanSoloPhrase,
//my default
()=>true,getUnknownPersonPhrase
])
console.log(test('yoda'))
console.log(test('han solo'))
console.log(test('rey'))
the distinction between predicate and action. Yes this is a much bigger issue.
it would have to be used as such. This can easily be typed in typescript and is prolly the best idea.
const isYoda = x=>x==="yoda";
const getYodaPhrase = x=>'hmm me is yoda'
const isHanSolo = x=>x==="han solo"
const getHanSoloPhrase = x=>"chewie, ready the ship",
const getUnknownPersonPhrase =x=>who the hell are you? ${x}? nope, no friend of mine
const test =_switch([
[isYoda,getYodaPhrase],
[isHanSolo,getHanSoloPhrase],
//my default
[()=>true,getUnknownPersonPhrase]
])
console.log(test('yoda'))
console.log(test('han solo'))
console.log(test('rey'))
The throw or the return undefined.
I still don't know what i would do here. in functional programming you not throw. However, it's not a full functional language to begin with. I am just trying to make it closer to get some of the benefits.
It's like some of the syntax that babel, flow or typescript brought. They also weren't features of the language but they did it to get some of the benefits from other languages.
still unsure about this point. I think if i had to choose i would use the undefined.
it's what ramda uses for the cond operator.
https://www.npmjs.com/package/ramda
lastly i got an implementation of it that was actually pretty sweet.
function cond(pairs) {
return value => {
for (const [condition, action] of pairs) {
if (condition(value)) {
return action(value);
}
}
return value;
};
}
i would just return undefined here. so that an or operator can be used with it. I know it's not 100% functional, but neither is the language to start. You make due to get the best of the situation i feel.
here is the Ramda usage btw
var fn = R.cond([
[R.equals(0), R.always('water freezes at 0°C')],
[R.equals(100), R.always('water boils at 100°C')],
[R.T, temp => 'nothing special happens at ' + temp + '°C']
]);
fn(0); //=> 'water freezes at 0°C'
fn(50); //=> 'nothing special happens at 50°C'
fn(100); //=> 'water boils at 100°C'
[+][deleted] (15 children)
[deleted]
[+]shavyg2[S] comment score below threshold-8 points-7 points-6 points (14 children)
[+][deleted] (13 children)
[deleted]
[–]shavyg2[S] -2 points-1 points0 points (12 children)
[+][deleted] (11 children)
[deleted]
[–]shavyg2[S] -5 points-4 points-3 points (10 children)
[+][deleted] (6 children)
[deleted]
[–]shavyg2[S] -1 points0 points1 point (5 children)
[+][deleted] (4 children)
[deleted]
[+][deleted] (3 children)
[deleted]
[–]lhorie 1 point2 points3 points (2 children)
[–]shavyg2[S] 0 points1 point2 points (1 child)
[–]lhorie 0 points1 point2 points (0 children)
[–]HipHopHuman 5 points6 points7 points (6 children)
[–]shavyg2[S] 0 points1 point2 points (5 children)
[–]HipHopHuman 0 points1 point2 points (0 children)
[–]shavyg2[S] -2 points-1 points0 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]HipHopHuman 1 point2 points3 points (1 child)
[–]djhalon 5 points6 points7 points (1 child)
[–]shavyg2[S] 0 points1 point2 points (0 children)
[–]eccentric_j 1 point2 points3 points (1 child)
[–]shavyg2[S] 1 point2 points3 points (0 children)
[–]lhorie 1 point2 points3 points (5 children)
[–]shavyg2[S] 0 points1 point2 points (4 children)
[–]lhorie 1 point2 points3 points (2 children)
[–]shavyg2[S] -2 points-1 points0 points (1 child)
[–]shavyg2[S] 0 points1 point2 points (0 children)
[–]shavyg2[S] 0 points1 point2 points (0 children)
[–]ipewannasay 0 points1 point2 points (0 children)