Created
November 26, 2011 01:16
-
-
Save heuristicfencepost/1394789 to your computer and use it in GitHub Desktop.
Code samples for blog post on list comprehensions
This file contains hidden or 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
(defn euclidean [x y] (Math/sqrt (+ (Math/pow x 2) (Math/pow y 2)))) | |
(println (for [ | |
[x,y] | |
[[1,2],[2,3],[3,4]] | |
:when (> (euclidean x y) 3.0)] | |
[(euclidean x y),(Math/toDegrees (Math/atan (/ y x)))])) |
This file contains hidden or 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
toDegrees :: Floating a => a -> a | |
toDegrees rad = rad * 180 / pi | |
euclidean :: Floating a => a -> a -> a | |
euclidean x y = let foo = x ^^ 2 | |
bar = y ^^ 2 | |
in sqrt (foo + bar) | |
main = do | |
print [(euclidean x y,(toDegrees . atan) (y / x))|(x,y)<-[(1,2),(2,3),(3,4)],(euclidean x y) > 3.0] |
This file contains hidden or 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
from math import sqrt, atan, degrees | |
def euclidean(x,y): | |
return sqrt(x**2 + y**2) | |
print [(euclidean(x,y),degrees(atan(float(y)/x))) for (x,y) in [(1,2),(2,3),(3,4)] if euclidean(x,y) > 3.0] |
This file contains hidden or 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
import scala.math.{atan,pow,sqrt,toDegrees} | |
def euclidean(x:Int,y:Int) = sqrt(pow(x,2) + pow(y,2)) | |
val result = | |
for { | |
(x,y) <- List((1,2),(2,3),(3,4)) | |
if (euclidean(x,y) > 3.0) } | |
yield (euclidean(x,y),toDegrees(atan(y.toFloat/x))) | |
println(result) |
This file contains hidden or 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
(defn euclidean [x y] (Math/sqrt (+ (Math/pow x 2) (Math/pow y 2)))) | |
(println (for [ | |
[x,y] | |
[[1,2],[2,3],[3,4]] | |
:let [dist (euclidean x y)] | |
:when (> dist 3.0)] | |
[dist,(Math/toDegrees (Math/atan (/ y x)))])) |
This file contains hidden or 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
toDegrees :: Floating a => a -> a | |
toDegrees rad = rad * 180 / pi | |
euclidean :: Floating a => a -> a -> a | |
euclidean x y = let foo = x ^^ 2 | |
bar = y ^^ 2 | |
in sqrt (foo + bar) | |
main = do | |
print [(dist,(toDegrees . atan) (y / x))|(x,y)<-[(1,2),(2,3),(3,4)],let dist=euclidean x y,dist > 3.0] |
This file contains hidden or 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
import scala.math.{atan,pow,sqrt,toDegrees} | |
def euclidean(x:Int,y:Int) = sqrt(pow(x,2) + pow(y,2)) | |
val result = | |
for { | |
(x,y) <- List((1,2),(2,3),(3,4)) | |
dist = euclidean(x,y) | |
if dist > 3.0 } | |
yield (dist,toDegrees(atan(y.toFloat/x))) | |
println(result) |
This file contains hidden or 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
from math import sqrt, atan, degrees | |
data=[(1,2),(2,3),(3,4)] | |
print [(dist,degrees) for (dist,degrees) in [(sqrt(x**2 + y**2),degrees(atan(float(y)/x))) for (x,y) in data] if dist > 3.0] |
This file contains hidden or 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
from math import sqrt, atan, degrees | |
data=[(1,2),(2,3),(3,4)] | |
def somefunc(alist): | |
for (x,y) in alist: | |
dist = sqrt(x**2 + y**2) | |
if (dist > 3.0): | |
yield (dist,degrees(atan(float(y)/x))) | |
print list(somefunc(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment