Skip to content

Instantly share code, notes, and snippets.

@zhangchiqing
Created January 3, 2016 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangchiqing/1983bd7dd67f2a100a0d to your computer and use it in GitHub Desktop.
Save zhangchiqing/1983bd7dd67f2a100a0d to your computer and use it in GitHub Desktop.
Implement list comprehension
/*
* 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