Skip to content

Instantly share code, notes, and snippets.

@modalsoul
modalsoul / ExtendedOption.hx
Created July 26, 2014 04:36
Option.getOrElse() like Scala in Haxe.
import haxe.ds.Option;
class ExtendedOption {
public static function getOrElse<T>(opt:Option<T>, defValue:T) {
return switch(opt) {
case Some(v): v;
case None: defValue;
}
}
}
object Catalan {
def myCatalan(m:Int, n:Int, accum:Int):Int = {
(m>0 && n>0, m==n) match {
case (false, _) => accum
case (_, true) => myCatalan(m, n-1, accum + 1)
case (_, false) => myCatalan(m-1, n, accum + 1)
}
}
}
defmodule Crypt do
def encrypt(numbers) do
min = Enum.min(numbers)
(product(numbers, 1)/min)*(min + 1)
end
def product([h|t],res), do: product(t, res * h)
def product([],res), do: res
end
IO.puts to_string(Crypt.encrypt([1,2,3]))
class Lylic(num:Int) {
val currentPlural = plural(num)
val nextPlural = plural(num-1)
def plural(x:Int) = {
x match {
case 0 => "No more bottles"
case 1 => "1 bottle"
case _ => x + " bottles"
}
@modalsoul
modalsoul / bottles.py
Created February 27, 2014 03:32
Pythonで99 bottles
#!/usr/bin/python
def plural(num):
if num == 0:
return "No more bottles"
elif num == 1:
return "1 bottle"
else:
return str(num) + " bottles"
def overLylic(num):
@modalsoul
modalsoul / PrimeNumCounter.ex
Created October 15, 2013 15:05
elixirで2数値間の素数の個数判定
defmodule PrimeNumCounter do
def count(sNum,eNum) do
list = [2|makeList(eNum, [])]
res = getPrimeList(list, [], trunc(:math.sqrt(eNum)))
Enum.count(Enum.filter(res, fn(x)-> x >= sNum end))
end
def getPrimeList([h|t], result, threshold) do
if threshold >= h do
getPrimeList(sieve(h, t, []), [h|result], threshold)
else
@modalsoul
modalsoul / PrimeNumCounter.ex
Created October 15, 2013 15:04
elixirで素数の個数判定
defmodule PrimeNumCounter do
def count(num) do
list = [2|makeList(num, [])]
res = getPrimeList(list, [], trunc(:math.sqrt(num)))
Enum.count(res)
end
def getPrimeList([h|t], result, threshold) do
if threshold >= h do
getPrimeList(sieve(h, t, []), [h|result], threshold)
else
@modalsoul
modalsoul / PrimeNum.ex
Last active December 25, 2015 11:39
elixirで素数判定
defmodule PrimeNum do
def isPrime(num) do
if num==1 || rem(num, 2)==0 do
false
else
x = makeList(trunc(:math.sqrt(num)), [])
sieve(x, num)
end
end
def sieve([h|t], num) do
@modalsoul
modalsoul / PrimeNumCounter.scala
Last active December 2, 2016 09:51
Scalaで2数値間の素数の個数検出
import scala.math.sqrt
import scala.collection.mutable.ArrayBuffer
object PrimeNumCounter {
def main(args:Array[String]) {
println(countPrimeNum(args(0).toInt, args(1).toInt))
}
def countPrimeNum(start:Int, end:Int) = {
var primeList:ArrayBuffer[Int] = ArrayBuffer[Int](2)
@modalsoul
modalsoul / PrimeNum.scala
Created October 10, 2013 13:57
Scalaで素数判定その3
import scala.math.sqrt
object PrimeNum {
def main(args:Array[String]) {
val num:Long = args(0).toLong
if(isPrime(num)) println(num + " is Prime Number.")
else println(num + " is NOT Prime Number.")
}