Skip to content

Instantly share code, notes, and snippets.

@faermanj
Created March 28, 2012 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save faermanj/2226280 to your computer and use it in GitHub Desktop.
Save faermanj/2226280 to your computer and use it in GitHub Desktop.
exploring-scala-1
def + + [B>: A, That] (that: TraversableOnce [B]) (implicit bf: CanBuildFrom [List [A], B, That]): That
object FiboFunctional {
val fibs: Stream[BigInt] =
0 #:: 1 #:: (fibs zip fibs.tail).map { case (a, b) => a + b }
def main(args: Array[String]) = println(fibs(1000));
}
public class FiboJava {
private static BigInteger fibo(int x) {
BigInteger a = BigInteger.ZERO;
BigInteger b = BigInteger.ONE;
BigInteger c = BigInteger.ZERO;
for (int i = 0; i < x; i++) {
c = a.add(b);
a = b;
b = c;
}
return a;
}
public static void main(String args[]) {
System.out.println(fibo(1000));
}
}
object FiboScala {
def fibo(x: Int): BigInt = {
var a: BigInt = 0;
var b: BigInt = 1;
var c: BigInt = 0;
for (_ <- 1 to x) {
c = a + b;
a = b;
b = c;
}
return a;
}
def main(args: Array[String]) = println(fibo(1000))
}
public class ListMACsJava {
public static void main(String[] args) throws SocketException {
Enumeration<NetworkInterface> nics =
NetworkInterface.getNetworkInterfaces();
for (NetworkInterface nic : Collections.list(nics)) {
byte[] mac = nic.getHardwareAddress();
for (int i = 0; mac != null && i < mac.length; i++) {
System.out.format("%2x", mac[i]);
System.out.print(i == mac.length - 1 ? '\n' : ':');
}
}
}
}
object ListMACsScala {
def main(args: Array[String]) {
NetworkInterface
.getNetworkInterfaces
.flatMap(nic => Option(nic.getHardwareAddress))
.map(_ map("%02x" format _) mkString ":")
.foreach(println(_));
}
}
//Type Definition
abstract class IntQueue {
def get(): Int
def put(x: Int)
def size(): Int
}
//ArrayBuffer implementation
class IntQueueImpl extends IntQueue {
private val buf = new ArrayBuffer[Int];
def get = buf.remove(0)
def put(x: Int) { buf += x }
def size = buf.length
}
trait Doubling extends IntQueue {
abstract override def put(x: Int) { super.put(2 * x) }
}
trait Incrementing extends IntQueue {
abstract override def put(x: Int) { super.put(x + 1) }
}
trait Filtering extends IntQueue {
abstract override def put(x: Int) { if (x > 0) super.put(x) }
}
//Mixing traits in type definition
class DoublePlusOneQueue extends IntQueueImpl with Incrementing with Doubling
object FilaComTrait {
def main(args: Array[String]) {
val queue1 = new DoublePlusOneQueue;
queue1.put(1);
queue1.put(2);
println(queue1.get());
println(queue1.get());
//Mixing traits in object instantiation
val queue2 = new IntQueueImpl with Filtering
queue2.put(-1);
queue2.put(1);
println(queue2.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment