Skip to content

Instantly share code, notes, and snippets.

View raymcdermott's full-sized avatar

Ray McDermott raymcdermott

  • OpenGrail
  • Belgium
View GitHub Profile
@raymcdermott
raymcdermott / gist:823982
Created February 12, 2011 18:49
For loop over map in Java 1.6
private Map<CartItem, Integer> cart = new HashMap<CartItem, Integer>();
public Double getTotalItemFees() {
Double fees = 0.0;
for (Map.Entry<CartItem, Integer> entry : cart.entrySet()) {
Integer quantity = entry.getValue();
CartItem cartItem = entry.getKey();
fees += (quantity * cartItem.getFee())
}
return fees;
@raymcdermott
raymcdermott / gist:823987
Created February 12, 2011 18:55
Java (OO) style
private val items = new HashMap[CartItem, Int]
def getTotalItemFees : Double = {
var sum = 0.0
for (item <- items)
sum += (item._1.getFee() * item._2)
sum
}
@raymcdermott
raymcdermott / gist:823989
Created February 12, 2011 18:58
For loop functional style:
def getTotalItemFees = {
val fees =
for (item <- items)
yield item._1.getFee() * item._2
fees.sum // High order function on the list that came out of the for loop
}
@raymcdermott
raymcdermott / gist:823990
Created February 12, 2011 19:00
Condensed for loop and high order function
def getTotalItemFees = {
( for (item <- items) yield item._1.getFee * item._2 ).sum
}
@raymcdermott
raymcdermott / gist:823995
Created February 12, 2011 19:02
High order function, pure functional style
def getTotalItemFees = {
items.foldLeft(0.0d)((accum, item) => accum + (item._1.getFee * item._2)
}
@raymcdermott
raymcdermott / gist:824010
Created February 12, 2011 19:09
\ is an alias for foldLeft and / is an alias for foldRight
def getTotalItemFees = {
items.\(0.0d)((accum, item) => accum + (item._1.getFee * item._2)
}
@raymcdermott
raymcdermott / gist:908731
Created April 7, 2011 21:11
Recursion with locally recursive function with default and named arguments
def scrubOut(line: String, keys: List[String]): String = {
def containsKeys(found: Boolean = false, local_line: String = line, local_keys: List[String] = keys): Boolean = {
if (local_keys.isEmpty || found) found
else {
containsKeys(line.contains(local_keys.head), local_line, local_keys.tail)
}
}
if (!line.contains("=") || !line.contains("\"") || keys.isEmpty || containsKeys() == false)
@raymcdermott
raymcdermott / gist:908742
Created April 7, 2011 21:15
simple recursion
def scrubOut(line: String, keys: Seq[String]): String = {
// Optimizations... most likely to the left
if (!line.contains("=") || !line.contains("\"") || keys.isEmpty || containsKeys(false, line, keys) == false)
line
else
scrubOut(line.replaceAll(keys.head + "=\".*?\"", keys.head + "=\"****\""), keys.tail)
}
@raymcdermott
raymcdermott / gist:908753
Created April 7, 2011 21:21
recursive java
public String scrubOut(String line, Collection<String> keys) {
if (keys.isEmpty()) return line;
else {
String key = keys.iterator().next();
keys.remove(key);
return scrubOut(line.replaceAll(key + "=\".*?\"", key + "=\"****\""), keys);
}
}
@raymcdermott
raymcdermott / gist:f4ec1166bf48d02fb68e
Created February 1, 2015 21:39
request-promise fn composition
var express = require('express'),
reqpromise = require('request-promise');
function register(req, res, next) {
var user = {};
user.username = req.body.username;
user.password = req.body.password;
user.firstName = req.body.firstName;
user.lastName = req.body.lastName;
user.email = req.body.username;