Skip to content

Instantly share code, notes, and snippets.

@earthengine
earthengine / rust-making-closures.csv
Last active January 30, 2019 23:10
Ways to make different closures in rust
move keyword State access State copiable Closure type
Yes By val No ClosureOnce
Yes By mut No ClosureUniq
Yes By ref No ClosureRef
Yes By mut* Yes ClosureMutCopy
Yes By val* Yes ClosureCopy
Yes By ref* Yes ClosureCopy
Yes No state N/A Function
No By val No ClosureOnce
No By mut No ClosureUniq
@earthengine
earthengine / rust-closures.csv
Last active January 30, 2019 23:23
Rust closures
Name Call mode State mutate Copy Other ability Traits can be implemented Traits implemented
Function Parallel/reentranted No Yes Cast to fn Fn + FnMut + FnOnce + Copy Fn + FnMut + FnOnce + Copy
ClosureMutCopy Parallel/reentranted Yes Yes State snapshot Fn + FnMut(multiple) + FnOnce + Copy FnMut + FnOnce + Copy
ClosureCopy Parallel/reentranted No/Meaningless Yes N/A Fn + FnMut + FnOnce + Copy Fn + FnMut + FnOnce + Copy
ClosureRef Parallel/reentranted No No N/A Fn + FnMut + FnOnce Fn + FnMut + FnOnce
ClosureUniq Sequencial Yes No N/A FnMut + FnOnce FnMut + FnOnce
ClosureOnce Once only Meaningless No N/A FnOnce FnOnce
@earthengine
earthengine / PushParser.hs
Created December 16, 2016 10:34
Haskell PushParser
module PushParser where
import Data.Monoid
import Control.Applicative
import Control.Monad
{-
t is the type of tokens, r is the type of results.
Empty result set means the parsing do not have a result at the moment.
public static class EmptyHandlers
{
public static void EmptyHandler<T, U>(T s, U e) { }
}
public static class EntityExt
{
private static string PropertyFromMemberExpression(MemberExpression memberExpression)
{
var pinfo = memberExpression.Member as PropertyInfo;
public static class NotificationExtensions
{
private static string PropertyFromMemberExpression(MemberExpression memberExpression)
{
var pinfo = memberExpression.Member as PropertyInfo;
if (pinfo != null)
{
return pinfo.Name;
}
var finfo = memberExpression.Member as FieldInfo;
public class UIThreadDrawingCanvas : FrameworkElement, IZoomFit
{
public static readonly DependencyProperty ContentDomainProperty = DependencyProperty.Register(
"ContentDomain", typeof(Rect), typeof(UIThreadDrawingCanvas));
public static readonly DependencyProperty RenderRequestProperty = DependencyProperty.Register(
"RenderRequest", typeof(ContextDrawer), typeof(UIThreadDrawingCanvas),
new FrameworkPropertyMetadata(null, RenderRequestChanged));
public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register(
"Background", typeof(Brush), typeof(UIThreadDrawingCanvas),
new FrameworkPropertyMetadata(Brushes.Transparent));
@earthengine
earthengine / AsyncRenderingCanvas.cs
Created October 7, 2016 13:16
A WPF custom control that allows drawing the content in a thread other than the main
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace WPFZoomTest
{
@earthengine
earthengine / ZoomViewer.xaml
Last active September 17, 2016 14:14
A WPF control that can zoom in and positioning its content by mouse
<UserControl x:Class="WPFZoomText.ZoomViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFZoomText"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500">
<Grid Background="Black" MouseDown="Grid_MouseDown" MouseUp="Grid_MouseUp" MouseWheel="Grid_MouseWheel"
SizeChanged="Grid_SizeChanged">
@earthengine
earthengine / cps.cs
Created July 30, 2015 02:25
Transforming c# expression tree in CPS
class CPSExpressionVisitor : ExpressionVisitor
{
delegate R SFunc<T, R>(T v);
delegate SFunc<T2, R> SFunc<T1, T2, R>(T1 v);
delegate SFunc<T2, T3, R> SFunc<T1, T2, T3, R>(T1 v);
delegate SFunc<T2, T3, T4, R> SFunc<T1, T2, T3, T4, R>(T1 v);
delegate dynamic FDynamic(FDynamic s);
private static FDynamic ConstFDynamic<T>(T v)
@earthengine
earthengine / DisposableLazy.cs
Last active August 29, 2015 14:24
Sometimes we need a disposable object to build a lazy value. If the lazy value was never accessed we need a way to dispose the object used for evaluation. This is a wrapper that will help on this
/// <summary>
/// A simple wrapper of a <see cref="Lazy"/> type that require a <see cref="IDisposable"/> object
/// to evaluate its value. The resulting object is also disposable.
///
/// Note: provides only 3 constructors. The 3 of <see cref="Lazy"/> that depends on the default
/// constructure does not make sense here.
/// </summary>
/// <typeparam name="T">The type of the disposable object</typeparam>
/// <typeparam name="R">The type of the lazily evaluated value</typeparam>
public class DisposableLazy<T, R> : IDisposable where T:IDisposable