Important Bits

Proverb Permutations

May 20, 2018

Let’s use rxjs just for fun. Maybe there’s a better operator for this than map.

const p1 = ['tallest', 'squeeky', 'early', 'second']
const p2 = ['nail', 'wheel', 'bird', 'mouse']
const p3 = ['hammer', 'grease', 'worm', 'cheese']

Rx.Observable.from(p1)
  .map(p1v => Rx.Observable.from(p2)
       .map(p2v => Rx.Observable.from(p3)
            .map(p3v => console.log(`${p1v} ${p2v} gets the ${p3v}`))))

Rmember to use Observable.from instead of Observable.of. of will not split in the array into a stream of individual values. It just emits the enitre array once as one value.

This code results in:

the tallest nail gets the hammer
the tallest nail gets the grease
the tallest nail gets the worm
the tallest nail gets the cheese
the tallest wheel gets the hammer
the tallest wheel gets the grease
the tallest wheel gets the worm
the tallest wheel gets the cheese
the tallest bird gets the hammer
the tallest bird gets the grease
the tallest bird gets the worm
the tallest bird gets the cheese
the tallest mouse gets the hammer
the tallest mouse gets the grease
the tallest mouse gets the worm
the tallest mouse gets the cheese
the squeeky nail gets the hammer
the squeeky nail gets the grease
the squeeky nail gets the worm
the squeeky nail gets the cheese
the squeeky wheel gets the hammer
the squeeky wheel gets the grease
the squeeky wheel gets the worm
the squeeky wheel gets the cheese
the squeeky bird gets the hammer
the squeeky bird gets the grease
the squeeky bird gets the worm
the squeeky bird gets the cheese
the squeeky mouse gets the hammer
the squeeky mouse gets the grease
the squeeky mouse gets the worm
the squeeky mouse gets the cheese
the early nail gets the hammer
the early nail gets the grease
the early nail gets the worm
the early nail gets the cheese
the early wheel gets the hammer
the early wheel gets the grease
the early wheel gets the worm
the early wheel gets the cheese
the early bird gets the hammer
the early bird gets the grease
the early bird gets the worm
the early bird gets the cheese
the early mouse gets the hammer
the early mouse gets the grease
the early mouse gets the worm
the early mouse gets the cheese
the second nail gets the hammer
the second nail gets the grease
the second nail gets the worm
the second nail gets the cheese
the second wheel gets the hammer
the second wheel gets the grease
the second wheel gets the worm
the second wheel gets the cheese
the second bird gets the hammer
the second bird gets the grease
the second bird gets the worm
the second bird gets the cheese
the second mouse gets the hammer
the second mouse gets the grease
the second mouse gets the worm
the second mouse gets the cheese

Adam Sullovey

Written by Adam Sullovey, powered by Gatsby.
Find me on codepen, github, or at Toronto meetups.