Skip to content

Instantly share code, notes, and snippets.

@TomasMikula
TomasMikula / README.md
Last active October 6, 2023 12:14
Demo: Lazy evaluation of the writer monad

The output from ghci demonstrates that wa >>= f produces the result even though wa contains diverging computations.

% ghci lazy-writer.hs
GHCi, version 9.4.4: https://www.haskell.org/ghc/  :? for help
[1 of 2] Compiling Main             ( lazy-writer.hs, interpreted )
Ok, one module loaded.
ghci> go
False, 42
ghci> 
@TomasMikula
TomasMikula / Replace.scala
Created May 19, 2015 05:23
Map a single type in a Coproduct. Lift Function1 to work on Coproducts.
import shapeless._
/** In coproduct `C`, replace `A` with `B`. */
trait Replace[C <: Coproduct, A, B] extends DepFn2[C, A => B] {
type Out <: Coproduct
def lift(f: A => B): C => Out =
c => apply(c, f)
}
@TomasMikula
TomasMikula / Leaky.java
Last active March 2, 2019 22:51
Demonstration of a memory leak when relying on WeakInvalidationListener in cases when the value of ObservableValue it is attached to never changes. Two possible solutions are presented as well.
import javafx.beans.property.SimpleDoubleProperty;
public class Leaky {
public static void main(String[] args) throws InterruptedException {
SimpleDoubleProperty a = new SimpleDoubleProperty(0.0);
for(int i = 0; i < 1000000; ++i) {
a.add(5).multiply(10).dispose();
}
reportMemoryUsage();
@TomasMikula
TomasMikula / MouseStationaryEvent.java
Last active March 2, 2019 22:51
Detecting when the mouse stays still over a node in JavaFX
import javafx.event.EventTarget;
import javafx.event.EventType;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.InputEvent;
public abstract class MouseStationaryEvent extends InputEvent {
private static final long serialVersionUID = 1L;
@TomasMikula
TomasMikula / LICENSE.md
Last active April 27, 2022 22:48
Mapped view of a JavaFX ObservableList.

CC0

To the extent possible under law, Tomas Mikula has waived all copyright and related or neighboring rights to this work.

@TomasMikula
TomasMikula / ChangeSubscriber.java
Created January 22, 2014 12:21
Helper classes for JavaFX to repeatedly register and unregister the same listener, while avoiding duplicate registration of the listener.
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
public final class ChangeSubscriber<T> {
private final ObservableValue<T> observable;
private final ChangeListener<T> listener;
private boolean isSubscribed;
public ChangeSubscriber(ObservableValue<T> observable, ChangeListener<T> listener) {
@TomasMikula
TomasMikula / Lazy.scala
Created November 26, 2013 01:14
lazily { expr }: Scala lazy vals that can be checked for having been evaluated. It is adapted from this StackOverflow answer: http://stackoverflow.com/a/17057490/1572599
import scala.language.implicitConversions
object Lazy {
def lazily[A](f: => A): Lazy[A] = new Lazy(f)
implicit def evalLazy[A](l: Lazy[A]): A = l()
}