Skip to content

Instantly share code, notes, and snippets.

View nachivpn's full-sized avatar

Nachi Valliappan nachivpn

View GitHub Profile
@nachivpn
nachivpn / findOriginalPrice.java
Last active July 30, 2017 18:34
findOriginalPrice.java
Map<String,Double> prices;
//return price of component if available, else return exception
public Either<Exception,Double> findOriginalPrice(String componentId){
Optional<Double> optionalPrice = Optional.ofNullable(prices.get(componentId));
return optionalPrice
.<Either<Exception,Double>>map(price -> new Right<>(price))
.orElse(new Left<>(new Exception("Price not found for " + componentId)));
}
@nachivpn
nachivpn / EitherPatternMatching.java
Last active July 30, 2017 18:28
EitherPatternMatching
//return a printable string containing component details, exception if component is unavailable
public String print(Either<Exception, Component> componentResp){
return componentResp.match(
(Exception ex) -> "Failed because " + ex.getMessage(),
(Component p) -> "Component info: " + p.prettyResult());
}
@nachivpn
nachivpn / EitherPartial.java
Last active July 30, 2017 18:26
EitherPartial.java
public abstract class Either<L, R>{
//used for pattern matching on contained value and processing/transforming it using some function
public abstract <T> T match(Function<L, T> a, Function<R, T> b);
//used to apply/compose functions which accapet a R (right) value and return an Either
//very useful to chain "either computations"
public abstract <S> Either<L, S> bind(Function<R, Either<L, S>> f);
//used to apply a simple generic R to S function on the R value contained inside an Either
@nachivpn
nachivpn / getComponent.java
Last active July 30, 2017 18:27
getComponent.java
Map<String,Component> components;
public interface Component{
String prettyResult();
String getId();
}
//returns component if available, exception otherwise
public Either<Exception, Component> getComponent(String componentName){
Optional<Component> optionalComponent = Optional.ofNullable(components.get(componentName));
return optionalComponent
//component available, return it using Right
@nachivpn
nachivpn / fade-wpool.erl
Created July 29, 2017 15:03
fade-wpool.erl
-module(wpool).
-export([init_pool/0,submit/1,get_result/1]).
-import(util,[make_id/1]).
-record(task, {sid, tid = "", work}).
%% API FUNCTION
% submit: W() -> Task_Id
submit(W) ->
MartinId = spawn_link(
@nachivpn
nachivpn / fade_py_ex.py
Created July 29, 2017 14:36
fade_py_ex.py
from Fade import FadeExecutor
def x(a, b): return [a + b, a - b]
with FadeExecutor() as f:
future = f.submit(x, 5, 3)
result = future.result()
-module(worker_pool).
-compile(export_all).
worker_pool(Funs) ->
Nodes = [node() | nodes()],
Master = self(),
Workers = lists:flatten([initNode(Node,Master,10) || Node <- Nodes]),
Result = master(Funs),
[ receive {get_work,W} -> W ! {no_work,normal} end || W<-Workers],
Result.
module PBKDF2 (
pbkdf2, pbkdf2HmacSha1
) where
import Data.HMAC (hmac_sha1)
import Text.Bytedump (dumpRaw)
import Control.DeepSeq
import OctetString
pbkdf2 :: (OctetString -> OctetString -> OctetString) -- pseudo random function (HMAC)
; int n = 25;
; int[] a = new int[n];
; return a.length;
; Generates this:
; ......
entry0:
;;; int n = 25;
@nachivpn
nachivpn / llvm-arrays.ll
Created May 10, 2017 18:26
Iplementing Arrays in LLVM
; In JL: int[] a = new int[2];
; In C: int *a = (int*) malloc(2 * sizeof(int));
; In LLVM:
%a = alloca i32*, align 8
; type of %a = i32 **
; %a is the location on stack which (later) holds the address of the array on the heap
%call = call i8* @malloc(i64 8) ; notice the size = 8 i.e, (2 * 4) as int is 4 bytes long