Skip to content

Instantly share code, notes, and snippets.

View ElectricCoffee's full-sized avatar

Niko Lepka ElectricCoffee

View GitHub Profile
@ElectricCoffee
ElectricCoffee / Collection Comparison.scala
Last active August 29, 2015 13:58
simple class enrichment for Seq, that allows you to perform boolean comparisons on them, these comparisons compare the sizes of the lists
// it's Seq[_] because we don't really care abou the type, all we're doing is comparing sizes anyway
implicit class RichSequence(a: Seq[_]) {
def ==(b: Seq[_]): Boolean = a.size == b.size
def !=(b: Seq[_]): Boolean = !(a == b)
def < (b: Seq[_]): Boolean = a.size < b.size
def <=(b: Seq[_]): Boolean = a < b || a == b
def > (b: Seq[_]): Boolean = !(a <= b)
def >=(b: Seq[_]): Boolean = !(a < b)
}
@ElectricCoffee
ElectricCoffee / while-else.scala
Last active August 29, 2015 13:58
the idea is to have a while-loop with an else statement
// what it tries to solve:
var cond = true
if(cond) {
while(cond) {
// do something
}
}
else {
// do something else
}
@ElectricCoffee
ElectricCoffee / Concept.txt
Last active August 29, 2015 14:00
A language concept that uses UML Class Diagram notation for the visibility and types, so + is public, - is private, # is protected, / is derived, _ is static, and ~ is a package. It's indentation based like Python and F#, and a block starts with a =. The only difference between a variable a class and a method, is that classes have members and me…
~ com.wausoft.test =
+ _ Program =
+ main(args: Array[String]): Unit = // unit is equivalent to void
printfn("this is a test")
- res = add(2, 4) // type inferred to int
printfn("result: $res") // example of string interpolation
+ add(a: Int, b: Int): Int = a + b
@ElectricCoffee
ElectricCoffee / stutter.txt
Last active August 29, 2015 14:00
An idea I had to make a lisp dialect, less focused on historical accuracy, and more focused on feeling like a modern language, and also having classes and objects be a central part of the language, rather than being "glued on" as it seems to be in some Lisp dialects
; Scala inspired Lisp
; I call it "Stutter" :3
; variable (mutable) definition:
(var a 5)
; value (immutable) definition:
(val b 4)
; function definition:
@ElectricCoffee
ElectricCoffee / SimpleHashGenerator.scala
Last active March 9, 2022 10:39
A very simple checksum generator written in scala, the following checksum types have been tested: MD2 MD5 SHA SHA-256 SHA-512
package your.pkg.here
import java.security.MessageDigest
import java.nio.file.{Files, Paths}
object Generator {
implicit class Helper(val sc: StringContext) extends AnyVal {
def md5(): String = generate("MD5", sc.parts(0))
def sha(): String = generate("SHA", sc.parts(0))
def sha256(): String = generate("SHA-256", sc.parts(0))
#!/bin/bash
function setup-sbt() {
# make the basic directories
mkdir -p "src/main/scala" # change scala to java if that's what you use
mkdir -p "src/main/resources"
mkdir -p "src/test/scala"
# make build file
touch build.sbt
@ElectricCoffee
ElectricCoffee / RichPath.scala
Last active August 29, 2015 14:03
A simple bit of extension methods that lets you write "someFolder" / "otherFolder" / "LastFolder" to create a java.io.File with that kind of path. An important feature of this, is that it's completely OS agnostic, meaning you don't have to worry about it being \ in Windows and / in Unix-based OSs, you just write it, and it handles the rest from …
import java.io.File
/**
* A completely OS-Agnostic way of dealing with java.io.File paths
*/
object RichPath {
sealed class RichBase[+A](left: A) {
/**
* Simple enrichment method designed to let you create a java.io.File
* by simply writing "folderA" / "folderB" / "folderC"
@ElectricCoffee
ElectricCoffee / ackermann.scala
Last active August 29, 2015 14:03
An attempt to calculate Ackermann in Scala... It keeps overflowing the stack near Ack(3,11)
package io.wausoft
import org.joda.time.{Seconds, DateTime}
import scala.collection.mutable
case class Ack(m: Int, n: Int)
object Program extends App {
// Basic ackermann without cache, overflows at around Ack(3,11) after a few seconds
def compute(a: Ack): Int = a match {
@ElectricCoffee
ElectricCoffee / then.scala
Last active May 27, 2016 22:58
Lets you call subroutines sequencially without code blocks, more info in the file
/*
* The premise of this is simple; it allows you to chain subroutines without using a block
* The idea comes from Haskell's Monads, where by language design you can't normally
* predict the order of statement execution due to the language being lazy,
* so instead an operator was added that allowed you to call statements sequencially
*
* Rather than writing something like this:
* def printStuff(): Unit = {
* println("blah")
* println("bloo")
@ElectricCoffee
ElectricCoffee / 0_program.scala
Last active January 4, 2016 13:02
Based on Dr. James Grime's nontransitive dice (aka. "Grime Dice") I set to test if they actually work as advertised, by throwing every possible combination 1000 times and printing the result on-screen. (spoiler alert they work). The Die class works on all dice of all sizes, with any numeric value on the faces
package com.wausoft.core
import com.wausoft.dice.Die
import com.wausoft.dice.Die.Multiple
object Program extends App {
// define the five different Grime Dice
val red = new Die("red" , 4, 4, 4, 4, 4, 9)
val olive = new Die("olive" , 5, 5, 5, 5, 5, 0)
val blue = new Die("blue" , 7, 7, 7, 2, 2, 2)