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
# | |
# A crude demo of how IO can be pure yet achieve something. | |
# All functions are pure, including main(). Only __main__ has effects. | |
# | |
class IO: | |
def __init__(self, f): | |
self.f = f | |
def run(self, x): | |
return self.f(x) |
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 GeoHash { | |
import Base32._, Calation._ | |
val LAT_RANGE = (-90.0, 90.0) | |
val LON_RANGE = (-180.0, 180.0) | |
// Aliases, utility functions | |
type Bounds = (Double, Double) | |
def mid(b: Bounds) = (b._1 + b._2) / 2.0 |
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
/** | |
* Examples of writing mixed unit/property-based (ScalaCheck) tests. | |
* | |
* Includes tables and generators as well as 'traditional' tests. | |
* | |
* @see http://www.scalatest.org/user_guide/selecting_a_style | |
* @see http://www.scalatest.org/user_guide/property_based_testing | |
*/ | |
import org.scalatest._ |
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 org.scalatest._ | |
import prop._ | |
import org.scalacheck.Arbitrary._ | |
import org.scalacheck.Gen | |
/** | |
* Solutions to the ScalaCheck problem that shrinking failing values may generate | |
* invalid values, because the constraints of the generator are not respected. | |
* | |
* See also http://stackoverflow.com/questions/20037900/scalacheck-wont-properly-report-the-failing-case |
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
// An explanation of using the Option type in Scala rather than null (in languages such as Java) | |
// The equivalent in Haskell is the Maybe type | |
// The Option type is for values that might or might not exist - i.e. exactly the use case | |
// that null fulfills. It has two subtypes: Some[A] and None | |
// ( where A is a type parameter - i.e. Some is a generic type. ) | |
val yes = Some("Hello world!") | |
val no = None |
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.language.implicitConversions | |
object configs { | |
// Options (e.g. from a config file or system properties), as key-value strings | |
val opts = Map("min" -> "100", "path" -> "/home/user2") | |
// Automatically wrap the default values in a Some | |
// (or can omit this and specify None to indicate lack of a default value) | |
implicit def wrap[A](a: A) = Some(a) |
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 example | |
import java.util.Date | |
object NoNulls { | |
sealed trait Employee // sealed, so nobody else can create further subclasses | |
object Employee { | |
def apply(id: Int, name: String, start: Date=new Date()) = CurrentEmployee(id, name, start) // factory method |
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 random | |
import sys | |
while True: | |
for i in range(80): | |
sys.stdout.write(random.choice("\/|-")) | |
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 random | |
from random import randint | |
from winsound import Beep | |
while True: | |
Beep(randint(200,5000), 250) |
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
letters = 'abcdefghijklmnopqrstuvwxyz ' | |
jumbled = 'jklmnopqrstuvwxyzabcdefghi ' | |
# Long way | |
#decrypt_map = { } | |
#encrypt_map = { } | |
#for x in range(len(encrypted_letters)): | |
# encrypted_letter = encrypted_letters[x] | |
# unencrypted_letter = unencrypted_letters[x] | |
# decrypt_map[unencrypted_letter] = encrypted_letter |