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 scala.util.Random | |
class Game{ | |
val board = Array(Array("_", "_", "_"), | |
Array("_", "_", "_"), | |
Array("_", "_", "_")) | |
var currentPlayer = "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
//Experimenting with traits. | |
trait Volume { | |
//define some methods we need to use to implement the functionality of | |
//this trait, the class using this trait will need to implement these methods | |
//concretely. | |
def area() : Int | |
def height : Int //this can also be a val | |
//the functionality provided by this trait | |
def volume() = { |
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
val words = List("this", "is", "a", "list", "of", "strings") | |
//foldLeft function (curried) | |
var size = words.foldLeft(0)((sum, word) => sum + 1) | |
var charCount = words.foldLeft(0)((sum, word) => sum + word.length()) | |
println(size) | |
println(charCount) | |
//foldleft operator |
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 Censor{ | |
val defaultReplacements = Map("shoot" -> "pucky", "darn" -> "beans") | |
def replace(input:String):String = { | |
replace(input, defaultReplacements) | |
} | |
def replace2(input:String):String = { | |
replaceWithPattern(input, defaultReplacements) | |
} |
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 scala.actors._ | |
import scala.actors.Actor._ | |
import ddf.minim._; | |
import ddf.minim.ugens._ | |
val minim = new Minim(this.applet) | |
size(400, 250) |
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
-module(day1). | |
-export([count_words/1]). | |
-export([upto/1]). | |
-export([printRet/1]). | |
% 1. Write a function that uses recursion to count the number of words in a string | |
list_len([]) -> 0; | |
list_len(Input) -> | |
[H | Tail] = Input, |
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
% 1. Consider a list of keyword-value tuples. Write a function that accepts | |
% the list and a keyword and returns the associated value for that keyword | |
get(List, Key) -> | |
%filter the list and select the first tuple by destructuring | |
[{ MatchKey, MatchVal} | _ ] = lists:filter(fun({K, V}) -> K == Key end, List), | |
MatchVal. | |
getOrElse(List, Key, Default) -> | |
%filter the list and select the first tuple by destructuring |
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
% 2. Consider a shopping list that looks like [{item, quantity, price}, ...] | |
% Write a list comprehension that adds the total price of each item i.e. quantity * price | |
q2() -> | |
List = [{pencil, 5, 0.25}, {pen , 3, 1}, {paper, 10, 0.1}], | |
[{Item, Quantity, Price, Quantity * Price} || {Item, Quantity, Price} <- List]. | |
% [{pencil,5,0.250000,1.25000}, | |
% {pen,3,1,3}, | |
% {paper,10,0.100000,1.00000}] |
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
%3 Write a board that takes a tic-tac-toe board presented as a list or tuple | |
% Return the current state of the game (winner, no winner yet, tie, incomplete) | |
tictactoe(Board) -> | |
[S11, S12, S13, | |
S21, S22, S23, | |
S31, S32, S33] = Board, | |
Rows = [[S11, S12, S13], [S21, S22, S23], [S31, S32, S33]], | |
Cols = [[S11, S21, S31], [S12, S22, S32], [S13, S23, S33]], |
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
% 1. Monitor the translator service and restart it if it dies. | |
% (this is pretty much directly from the book) | |
-module(day3). | |
-export([loop/0, translate/2]). | |
-export([watch/0]). | |
% the translator service | |
loop() -> |