Skip to content

Instantly share code, notes, and snippets.

@mepcotterell
Created September 21, 2011 01:41
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 mepcotterell/1230977 to your computer and use it in GitHub Desktop.
Save mepcotterell/1230977 to your computer and use it in GitHub Desktop.
Examples of Unicode Operators using the ScalaTion DSL
object ExponentiationApp extends App with ScalaTion
{
val exp1 = 2 ↑ 2 // 4
val exp2 = 2 ↑ 2 ↑ 2 // 16
println("2↑2 = %s".format(exp1))
println("2↑2↑2 = %s".format(exp2))
}
object FactorialApp extends App with ScalaTion
{
val fac1 = 4! // 24
val fac2 = 3.5! // 11.63172...
println("4! = %s".format(fac1))
println("3.5! = %s".format(fac2))
}
object FactorialApp2 extends App with ScalaTion
{
val rising = 4 ⇑ 4 // 840
val falling = 4 ⇓ 4 // 24
println("4 ⇑ 4 = %s".format(rising))
println("4 ⇓ 4 = %s".format(falling))
}
object IntegralApp extends App with ScalaTion
{
val int1 = ∫(1 to 4, (i: Double) ⇒ i ↑ 2) // 21
println("∫(1 to 4, i ⇒ i ↑ 2) = %s".format(int1))
}
object ProductApp extends App with ScalaTion
{
val prod1 = ∏(1 to 3) // 6
val prod2 = ∏(1 to 3, (i: Int) ⇒ i ↑ 2) // 36
val vec = Vec(1, 2, 3, 4)
val prod3 = ∏(vec) // 24
val prod4 = ∏(0 to 2, (i: Int) ⇒ vec(i)) // 6
println("∏(1 to 3) = %s".format(prod1))
println("∏(1 to 3, i ⇒ i ↑ 2) = %s".format(prod2))
println("vec = %s".format(vec))
println("∏(vec) = %s".format(prod3))
println("∏(0 to 2, i ⇒ vec(i)) = %s".format(prod4))
}
object RootApp extends App with ScalaTion
{
val root1 = 4 ↓ 2 // 2
val root2 = 4 ↓ 2 ↓ 2 // 1.41421...
val test = 4 ↑ 0.5 == 4 ↓ 2 // true
println("4↓2 = %s".format(root1))
println("4↓2↓2 = %s".format(root2))
println("4↑0.5 == 4↓2 = %s".format(test))
}
object SetApp extends App with ScalaTion
{
val set1 = Set(1, 2, 3, 4)
val set2 = Set(1.0, 2.0, 3.0, 4.0)
val setTest1 = 2 ∈ set1 // true
val setTest2 = 5 ∈ set1 // false
val setTest3 = 2.0 ∈ set2 // true
println("set1 = %s".format(set1))
println("set2 = %s".format(set2))
println("2 ∈ set1 = %s".format(setTest1))
println("5 ∈ set1 = %s".format(setTest2))
println("2 ∈ set2 = %s".format(setTest3))
}
object SummationApp extends App with ScalaTion
{
val sum1 = ∑(1 to 3) // 6
val sum2 = ∑(1 to 3, (i: Int) ⇒ i ↑ 2) // 14
val sum3 = ∑(vec) // 10
val sum4 = ∑(0 to 2, (i: Int) ⇒ vec(i)) // 6
println("∑(1 to 3) = %s".format(sum1))
println("∑(1 to 3, i ⇒ i ↑ 2) = %s".format(sum2))
println("vec = %s".format(vec))
println("∑(vec) = %s".format(sum3))
println("∑(0 to 2, i ⇒ vec(i)) = %s".format(sum4))
}
object VectorApp extends App with ScalaTion
{
val vec1 = Vec(1, 2, 3)
val vec2 = Vec(4, 3, 2)
val vec3 = Vec(1, 2, 3, 4, 5, 6, 7)
val dp1 = vec1 ⋅ vec2 // 16
val v2 = vec3(2) // 3
val v3 = vec3(3) // 4
val v2_4 = vec3(2 to 4)
val v2_3 = vec3(2 until 4)
val v2_4_ = vec3(2 ⋯ 4)
println("vec1 = %s".format(vec1))
println("vec2 = %s".format(vec2))
println("vec1 ⋅ vec2 = %s".format(dp1))
println("vec3 = %s".format(vec3))
println("vec3(2) = %s".format(v2))
println("vec3(3) = %s".format(v3))
println("vec3(2 to 4) = %s".format(v2_4))
println("vec3(2 until 4) = %s".format(v2_3))
println("vec3(2 ⋯ 4) = %s".format(v2_4_))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment