Skip to content

Instantly share code, notes, and snippets.

@heuristicfencepost
Created November 26, 2011 01:16
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 heuristicfencepost/1394789 to your computer and use it in GitHub Desktop.
Save heuristicfencepost/1394789 to your computer and use it in GitHub Desktop.
Code samples for blog post on list comprehensions
(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)))]))
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]
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]
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)
(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)))]))
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]
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)
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]
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