This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; int n = 25; | |
; int[] a = new int[n]; | |
; return a.length; | |
; Generates this: | |
; ...... | |
entry0: | |
;;; int n = 25; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 |