Skip to content

Instantly share code, notes, and snippets.

Implement a function, routine or method (and the support code you might need) that simulates 6 tosses of 3 coins
each and outputs a string according to the Value Rules below.
Value Rules
a) Tails has a value of 2, and heads a value of 3.
b) Getthetotalvalueofthethreecoinspertoss.
c) Compose a string with these values in reverse order, from toss 6 to 1. Example output: “767777”
val r = scala.util.Random
object JsonParsing extends App {
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.JsonDSL._
implicit val formats = DefaultFormats
println(parse( """ { "numbers" : [1, 2, 3, 4] } """))
println(parse( """{"name":"Toy","price":35.35}""", useBigDecimalForDouble = true))
@mcalavera81
mcalavera81 / MessingAround.java
Created October 28, 2015 20:40
Strategy Pattern with Lambda expressions.
package com.example;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class MessingAround {
@mcalavera81
mcalavera81 / MessingAround.java
Last active October 29, 2015 14:29
Fluent API and Loan Pattern/Execute around method Pattern
package com.example;
import java.io.IOException;
import java.util.function.Function;
public class MessingAround {
public static void main(String[] args) throws IOException, InterruptedException {
@mcalavera81
mcalavera81 / MessingAround.java
Created October 30, 2015 14:22
Lazy evaluation
package com.example;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.function.Supplier;
public class MessingAround {
sleeping=1
if [ "$1" != "" ]; then
sleeping=$1
fi
echo " (___)"
echo " (o o)____/"
echo " @@ \\"
echo " \ ____, /"
echo " // //"
Arrays.stream(e.getStackTrace()).limit(5L).map(stackEl -> " " + stackEl).collect(Collectors.joining("\n")));
trait Combinable[T]{
def combine(a:T,b:T):T
}
object Combinable{
def apply[T](implicit c:Combinable[T]) = c
trait CombinableOps[T]{
def self:T
def combinable: Combinable[T]
def combine(other: T) = combinable.combine(self,other)
@mcalavera81
mcalavera81 / applicativeFunctorNodeJS
Last active June 14, 2016 18:00
Using Task as an applicative functor to write a file with the contents of two files. Supporting variable number of functor instances.
#!/usr/bin/env node
var Task = require('data.task')
var fs = require("fs");
var _ = require('ramda')
//readFile :: filename -> Task(Data)
const readFile = function(filename){
return new Task(function(reject, resolve) {
fs.readFile(filename, function (err, data) {
@mcalavera81
mcalavera81 / Promises-generators combo
Created June 21, 2016 13:57
Working with multiple promises in a linear synchronous manner with the help of generators and promises.
app.put('/api/bundle/:id/book/:pgid', function(req, res) {
let
get = Q.denodeify(request.get),
put = Q.denodeify(request.put);
Q.async(function* (){
let args, couchRes, bundle, book;
// grab the bundle from the b4 database
args = yield get(config.b4db + req.params.id);
couchRes = args[0];