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 ListMap { | |
def squareList(xs: List[Int]): List[Int] = xs match { | |
case List() => xs | |
case y :: ys => y * y :: squareList(ys) | |
} | |
def squareListMap(xs: List[Int]): List[Int] = | |
xs map (x => x * x) | |
def main(args: Array[String]) { | |
println("squarelist for square of (9,13,21) returns : " + squareList(List(9, 13, 21))); |
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 ListFilter { | |
def forall(xs: List[Int], p: Int => Boolean): Boolean = | |
xs.isEmpty || xs.filter(p) == xs | |
def exists(xs: List[Int], p: Int => Boolean): Boolean = | |
!xs.isEmpty && !xs.filter(p).isEmpty | |
def main(args: Array[String]) { | |
println("Are members of list (1,2,3,4,5) less than 10? : " + | |
forall(List(1,2,3,4,5), (x => x < 10))); | |
println("Is there at least one member of list (1,2,3,4,5) thats greater than 5? : " + |
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 NQueensProblem { | |
def queens(n: Int): List[List[Int]] = { | |
def placeQueens(k: Int): List[List[Int]] = | |
if (k == 0) List(List()) | |
else for { queens <- placeQueens(k - 1) | |
column <- List.range(1, n + 1) | |
if isSafe(column, queens, 1)} yield column :: queens | |
placeQueens(n) | |
} | |
def isSafe(col: Int, queens: List[Int], delta: Int): Boolean = queens match { |
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 ForToMap { | |
def main(args: Array[String]) { | |
case class Book(title: String, authors: List[String]) | |
val books: List[Book] = List( | |
Book("Structure and Interpretation of Computer Programs", | |
List("Abelson, Harold", "Sussman, Gerald J.")), | |
Book("Principles of Compiler Design", | |
List("Aho, Alfred", "Ullman, Jeffrey")), | |
Book("Programming in Modula2", | |
List("Wirth, Niklaus")), |
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 FunctionalLoops { | |
def repeatLoop(command: => Unit)(condition: => Boolean) { | |
command | |
if (condition) { | |
repeatLoop(command)(condition) | |
} else () | |
} | |
def repeatUntilLoop(command: => Unit) = { | |
class RepeatUntilLoopClass(command: => Unit) { |
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 SimpleOrGate { | |
type Action = () => Unit | |
class Wire { | |
private var sigVal = false | |
private var actions: List[Action] = List() | |
def getSignal = sigVal | |
def setSignal(s: Boolean) = | |
if (s != sigVal) { | |
sigVal = s |
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 CompoundOrGate { | |
type Action = () => Unit | |
class Wire { | |
private var sigVal = false | |
private var actions: List[Action] = List() | |
def getSignal = sigVal | |
def setSignal(s: Boolean) = | |
if (s != sigVal) { | |
sigVal = s |
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 rlcompleter, readline | |
readline.parse_and_bind('tab: complete') |
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 mergesort(arr, n): | |
if n == 1: return arr | |
len_half = int(n/2) | |
return merge(mergesort(arr[:len_half], len_half), | |
mergesort(arr[len_half:], n - len_half), n) | |
def merge(arr1, arr2, n): | |
result = [] | |
i,j = 0,0 | |
for x in range(n): |
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
""" | |
Search for a text in all repos for an organization. | |
Currently via the web UI you can only search per repo. | |
This will only return number of matches for the text and not | |
the filenames (as of now). | |
You need an OAuth Token for this to work. | |
Run it as: |