-
-
Save kevinwright/2258100 to your computer and use it in GitHub Desktop.
exploring-scala-1
This file contains 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
def + + [B>: A, That] (that: TraversableOnce [B]) (implicit bf: CanBuildFrom [List [A], B, That]): That |
This file contains 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
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)) | |
} |
This file contains 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
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)); | |
} | |
} |
This file contains 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
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 | |
} | |
a | |
} | |
def main(args: Array[String]) = println(fibo(1000)) | |
} |
This file contains 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
public class ListMACsJava { | |
public static void main(String[] args) { | |
try { | |
Enumeration<NetworkInterface> nics = | |
NetworkInterface.getNetworkInterfaces(); | |
while (nics.hasMoreElements()) { | |
NetworkInterface nic = nics.nextElement(); | |
byte[] mac = nic.getHardwareAddress(); | |
String macStr = ""; | |
if (mac != null) | |
for (int i = 0; i < mac.length; i++) { | |
macStr += String.format("%02x", mac[i]); | |
if (i < mac.length - 1) | |
macStr += ":"; | |
} | |
System.out.println(macStr); | |
} | |
} catch (SocketException e) { | |
System.err.println("Ooops!"); | |
} | |
} | |
} |
This file contains 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 collection.JavaConverters._ | |
import java.net.NetworkInterface | |
object ListMACsScala extends App { | |
val nicaddresses = for { | |
nic <- NetworkInterface.getNetworkInterfaces.asScala | |
addrbytes <- Option(nic.getHardwareAddress) | |
} yield { | |
addrbytes map {"%02x" format _} mkString ":" | |
} | |
nicaddresses foreach println | |
} |
This file contains 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
//Type Definition | |
trait IntQueue { | |
def get(): Int | |
def put(x: Int) | |
def size(): Int | |
} | |
//ArrayBuffer implementation | |
class IntQueueImpl extends IntQueue { | |
private[this] val buf = new collection.mutable.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) } | |
} |
This file contains 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
//Mixing traits in type definition | |
class DoublePlusOneQueue extends IntQueueImpl with Incrementing with Doubling | |
object FilaComTrait extends App { | |
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