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
//forked from https://gist.github.com/jkdeveyra/4030815 | |
def sentenceAnagrams(sentence: Sentence): List[Sentence] = { | |
def subSentence(occ: Occurrences): List[Sentence] = { | |
if (occ.isEmpty) List(List()) | |
else | |
for { | |
x <- combinations(occ) | |
y <- dictionaryByOccurrences.getOrElse(x, List()) |
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
// Inspired by Oderski's anagram and the idea that SICP count change algorithm can be used | |
// for anagram lookup and more efficiently than odersky wanted. Anagrams are however permu | |
// tated "changes" and, thus, we need an algorithm for permutation. There is none in stack | |
// overflow, at least I could not find out whether scala and other programmers provide per | |
// mutations with replacements or not whereas anagrams requre special kind of permutations | |
// It should be that "aab", "aba" and "baa" would be permutations. Note multiplicity 2 of | |
// 'a'. With replacements produces aaa since a is not exhausted by two repetitions wheras | |
// without repetitions cannot produce sequences longer than 2 since picked two items we a | |
// re out of items in the dictionary. If we put two a items in the dictionary, with/wo rep | |
// etitions will produce idential sequences, aab and aab can be produced two times. |
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 GenericChange { | |
// Part I: generic coin change algorithm | |
// SICP count change algorithm, http://mitpress.mit.edu/sicp/full-text/sicp/book/node16.html | |
def coin_change(sum: Int, coins: List[Int]): Int = { | |
if (sum == 0) 1 else | |
if (coins.isEmpty || sum < 0) 0 else | |
coin_change(sum - coins.head, coins) + coin_change(sum, coins.tail) |
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
class Args (val args: Array[String]) extends Enumeration { | |
class OptVal extends Val { | |
override def toString = "-" + super.toString | |
} | |
val nopar, silent, reportSamples = new OptVal() { // boolean options | |
def apply(): Boolean = args.contains(toString) | |
} | |
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
// Rumor says that they must match. They do indeed (I believe less than 1% mistmach is because | |
// tables are not 100% complete rather than invalid input data) | |
import math.pow | |
object student_vs_anova { | |
def log(msg: String) { | |
//println(msg) | |
} //> log: (msg: String)Unit | |
def fTest(alpha: Double, var1: Double, freedoms1: Int, var2: Double, freedoms2: Int) = { |
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
// Indeterministic chooses all possible states, maintaining probabilities | |
// Probabilistic chooses one of possible states on every transition. | |
object Probabilistic { | |
class Machine[T : Ordering] (transitions: Map[T, Set[T]]) { // TODO: probabilistic transitions | |
def step(history: List[T]) = { | |
val p2 = transitions(history.head) | |
val picked = (Math.random() * p2.size).toInt | |
p2.toList(picked) :: history |
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
File deletion does not have immediate effect at least process image file, at least when | |
process was killed ungracefully prematurely. I have to retry the delete in my app. | |
Also, EVEN IF FILE DELETED, IT IS REPORTED EXISTING and complire cannot reporduce the | |
file. I have to retry the compilation also. | |
object prog_gen extends App { | |
import scala.sys.process._, scala.concurrent._, ExecutionContext.Implicits.global, Paths.get | |
implicit def toPath(filename: String) = get(filename) |
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 generator { | |
// Coursera Reactive programming | |
trait Generator[T] { self => | |
def generate: T | |
def map[K](f: T => K) = new Generator[K] { | |
// {val sample = generate ; println("map creates a generator [" + sample.getClass().getSimpleName + "]") } | |
def generate: K = f(self.generate) | |
} |
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
// This talk https://www.youtube.com/watch?v=MGfr_col5PI claims that if 4% of healty people degenerate with every | |
// and if 50% recover from defective genetics then we reach 8% failed genetics. Here is the simulation that confirms this. | |
def sim1(healthy: Double, generation: Int = 1): Unit = if (generation < 100) { | |
val degenerates = 1 - healthy; println((degenerates * 100) + "% down" ) | |
sim1(healthy * 0.96 + degenerates * 0.5, generation + 1) | |
} //> sim1: (healthy: Double, generation: Int)Unit | |
sim1(1) //> 0.0% down | |
//| 4.0000000000000036% down |
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
trait E { | |
def e(dict: Map[Const, Int]): Int | |
def +(another: E): E = (this, another) match { | |
case (zero, _) => another // Bug! Bug! this unintentionally redefines zero | |
case (_, zero) => this | |
case _ => Plus(this, another) | |
} | |
def *(another: E): E = { | |
def par(e: E) = e match {case e @ Plus(a,b) => Par(e); case e => e} | |
Mult(par(this), par(another)) |
OlderNewer