Skip to content

Instantly share code, notes, and snippets.

@esfand
esfand / MonadicJava.md
Last active August 29, 2015 14:02
Monadic Java

What is a monad?

A monad is a triple (T, η, μ) where

  • T is an endofunctor T: X -> X and
  • η: I -> T and μ: T x T -> T are 2 natural transformations satisfying these laws:
    • Identity law: μ(η(T)) = T = μ(T(η))
    • Associative law: μ(μ(T × T) × T)) = μ(T × μ(T × T))

In other words:

a monad in X is just a monoid in the category of endofunctors of X, with

/**
* ```
* Does JDK8's Optional class satisfy the Monad laws?
* =================================================
* 1. Left identity: true
* 2. Right identity: true
* 3. Associativity: true
*
* Yes, it does.
* ```
@esfand
esfand / CovarianceTest.java
Last active August 29, 2015 14:02 — forked from mallipeddi/gist:74274
Demonstrates the concepts of covariance and contravariance in the Java type system
import java.util.*;
/*
* Covariance and contravariance.
*
* A type operator (an operator which takes as input a type and returns another type as output) is
* said to be
* 1) `covariant` if it preserves the ordering of types and orders types
* from more specific ones to more generic ones.
* 2) `contravariant` if it preserves the ordering of types and orders types

Differences between declaration-site and use-site variance

http://stackoverflow.com/questions/4231305/how-does-javas-use-site-variance-compare-to-cs-declaration-site-variance

First off, use-site variance is strictly more powerful than declaration-site variance (although at the cost of concision), or at least Java's wildcards are (which are actually more powerful than use-site variance). This increased power is particularly useful for languages in which stateful constructs are used heavily, such as C# and Java (but Scala much less so, especially since it's standard lists are immutable).

@esfand
esfand / typeinferenceJava.md
Last active August 29, 2015 14:02
Type Inference in Java

Emulating "self types" using Java Generics to simplify fluent API implementation

http://passion.forco.de/content/emulating-self-types-using-java-generics-simplif...

The Java programming language does not support the notion of "self types", that is "type of this". When implementing fluent APIs, this lack of support can lead to significant duplication of boilerplate code. Taking FEST Assertions as an example, this article shows how "self types" can be emulated in Java using Generics.

Why bother?

Monad

All it takes to be a Monad is to provide two functions which conform to three laws.

The two functions:

  1. Place a value into monadic context
  • Haskell's Maybe: return / Just
  • Scala's Option: Some
//This is an example of how to scrape the web using PhantomJS and jQuery:
//source: http://snippets.aktagon.com/snippets/534-How-to-scrape-web-pages-with-PhantomJS-and-jQuery
//http://phantomjs.org/
var page = new WebPage(),
url = 'http://localhost/a-search-form',
stepIndex = 0;
/**
* From PhantomJS documentation:
<!DOCTYPE html>
<!-- http://www.sivalabs.in/2014/09/angularjs-different-ways-of-using-array.html -->
<html ng-app="myApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My AngularJS App</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
package selenium;
import java.sql.Driver;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;