This file contains hidden or 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
val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map { n => n._1 + n._2 } | |
def fibs(x: BigInt=0, y: BigInt=1): Stream[BigInt] = x #:: fibs(y, x + y) | |
val fibi: Iterator[BigInt] = Iterator.iterate((0,1)){case (x,y) => (y,x+y)}.map(_._1) | |
def fibi(x: BigInt=0, y: BigInt=1): Iterator[BigInt] = Iterator.single(x)++fibi(y, x + y) | |
println(fibs.take(19).toList) | |
println(fibs().take(19).toList) |
This file contains hidden or 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
package influxdb | |
import org.influxdb._ | |
import org.influxdb.dto._ | |
import java.util.concurrent.TimeUnit | |
object Influx1 extends App { | |
val influxDB = InfluxDBFactory.connect("http://localhost:8086", "root", "root"); | |
println(influxDB.describeDatabases()) |
This file contains hidden or 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 closestNumbers(s:String):String={ | |
val sorted=s.split(" ").map(_.toInt).toList.sorted | |
val zip=sorted.zip(sorted.tail) | |
val triples=for { | |
(x,y)<-zip | |
} yield (x,y,Math.abs(x-y)) | |
val (_,_,minDelta)=triples.minBy(_._3) | |
This file contains hidden or 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 pack(s:String):List[String]= | |
if (s.isEmpty()) Nil | |
else{ | |
val (packed, rest) = s span { _ == s.head } | |
packed::pack(rest) | |
} | |
def rle(s:String)= pack(s).map(e=>(e.head,e.length)) | |
def compress(s:String):String= |
This file contains hidden or 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 isPangram(strings: Array[String]): String = { | |
val values=for{ | |
string<-strings | |
set=string.toLowerCase().toSet | |
isPangram=('a' to 'z').forall(set.contains) | |
} yield if (isPangram) 1 else 0 | |
values.mkString | |
} | |
This file contains hidden or 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 kSub(k:Int,nums:Array[Int]):Long={ | |
val count=for { | |
i<-0 to nums.size | |
j<-i to nums.size | |
slice=nums.slice(i, j) | |
sum=slice.sum | |
} yield if (sum!=0 && (sum%k)==0) 1 else 0 | |
count.sum | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8"?> | |
<document> | |
<people><person name="John" id="john"/><person name="Mary" id="mary"/></people> | |
<text>Hello John, say hi to Mary</text> | |
</document> |
This file contains hidden or 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 scala.xml._ | |
import scala.xml.transform._ | |
object RewriteRuleExample extends App { | |
val xml = | |
<document> | |
<people><person name="John" id="john"/><person name="Mary" id="mary"/></people> | |
<text>Hello John, say hi to Mary</text> | |
</document> |
This file contains hidden or 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 Robot extends App { | |
// proper Maze implementation should be here, not my concern | |
object Maze{ | |
def isDoorway(direction:Int):Boolean=true | |
def isTreasureRoom():Boolean=false | |
def move(directon:Int)={} | |
} | |
This file contains hidden or 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 stuff { | |
class Catalog(documentsIn:List[Document]=List.empty){ | |
def isEmpty=_documents.isEmpty | |
def add(document:Document)= _documents::=document | |
private[this] var _documents=documentsIn | |
def documents= _documents | |
} | |
class ImCatalog(val documents:List[Document]=List.empty){ |