Skip to content

Instantly share code, notes, and snippets.

View softarn's full-sized avatar

Marcus Höjvall softarn

View GitHub Profile
import java.util.Optional;
public class OptionalOrElse {
public static void main(String[] args) {
// Correct usage of orElseGet. createInventoryForUser is not invoked unless.
System.out.println("Using the APIs in a correct way");
findInventoryByUserId(1).orElseGet(() -> createInventoryForUser(1));
findInventoryByUserId(2).orElseGet(() -> createInventoryForUser(2));
@softarn
softarn / Declaration.java
Last active May 13, 2018 13:02
Blog - Optional.orElse
public T orElse(T other)
public T orElseGet(Supplier<? extends T> other)
@softarn
softarn / snowglobe_activator.py
Last active August 29, 2015 14:10
BLOG - Christmas snowglobe - Simulating a button press on the raspberry pi
import RPi.GPIO as GPIO
import urllib2
import simplejson
import time
import sys
GPIO.setmode(GPIO.BCM)
print "Setup Pin 10 to output"
GPIO.setup(10, GPIO.OUT)
for i in [1,2,3]:
print i
else:
print 4
#Will print 1 2 3 4
#But if you break the loop...
for i in [1,2,3]:
print i
@softarn
softarn / symbolize_nested_keys.rb
Created August 21, 2012 15:49
Symbolize keys for nested hashes, tested in ruby 1.8.7
def self.recursive_symbolize_keys! hash
hash.symbolize_keys!
inner = hash.values.select{|v| v.is_a? Hash}.each{|h| recursive_symbolize_keys!(h)}
inner.each {|inner| hash.merge(inner)}
hash
end