Created
January 3, 2016 22:10
-
-
Save zhangchiqing/1983bd7dd67f2a100a0d to your computer and use it in GitHub Desktop.
Implement list comprehension
This file contains 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
/* | |
* Example: | |
* Find any integer x, y, z that match the following conditions | |
* 1. 0 < x < y < z < 10 | |
* 2. x * x + y * y = z * z | |
* In Python: | |
* [(x,y,z) for x in range(1,10) for y in range(1,10) for z in range(1,10) if x < y and y < z and x * x + y * y == z * z] | |
* >>> [(3, 4, 5)] | |
* In Javascript with Ramda: | |
*/ | |
var R = require('ramda'); | |
R.pipe( | |
R.sequence(R.of), | |
R.filter(R.apply(function(x, y, z) { | |
return x < y && y < z && (x * x + y * y == z * z); | |
})) | |
)([R.range(1, 10), R.range(1, 10), R.range(1, 10)]) | |
// [[3, 4, 5]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment