Skip to content

Instantly share code, notes, and snippets.

View moiseev's full-sized avatar
🤷‍♂️

Max Moiseev moiseev

🤷‍♂️
  • San Diego, CA
  • 22:38 (UTC -08:00)
View GitHub Profile

Introduce Sequence.filteredMap(_:)

  • Proposal: SE-NNNN
  • Authors: Max Moiseev
  • Review Manager: TBD
  • Status: Awaiting implementation
@moiseev
moiseev / dividable.swift
Created October 6, 2017 18:17
If you really really miss the `/` from `Numeric`...
public protocol Dividable {
static func / (lhs: Self, rhs: Self) -> Self
}
extension Int : Dividable {}
extension Double : Dividable {}
extension Sequence where Element : Numeric & Dividable {
func average() -> Element {
var i: Element = 0
@moiseev
moiseev / newer-integers.md
Created January 13, 2017 20:43
Protocol-oriented integers (take 2)

Keybase proof

I hereby claim:

  • I am moiseev on github.
  • I am moiseev (https://keybase.io/moiseev) on keybase.
  • I have a public key whose fingerprint is 4CD1 7F10 0D47 6566 7D6F F955 5B0C F4A4 E73E 4354

To claim this, I am signing this object:

def append(xs, yss):
return [[x] + ys for x in xs for ys in yss]
def make_lists(*xss):
if len(xss) == 1:
return [[x] for x in xss[0]]
h, t = xss[0], xss[1:]
return append(h, make_lists(*t))
string[] strings = new string[] { "1", "123", "12", "12ab", "a", "ab", "abc" };
var res = strings.Where("Length >= 3");
type IClauseVisitor<'TState> =
abstract member Simple : String array * Condition * Val -> 'TState
abstract member Complex : 'TState * LogicalOperation * 'TState -> 'TState
let visitClause (visitor:#IClauseVisitor<'TState>) clause =
let simpleF props (cond:Cond) value = visitor.Simple(props, (cond.ToEnum()), value)
let complexF left (logOp:LogOp) right = visitor.Complex(left, (logOp.ToEnum()), right)
Clause.Fold simpleF complexF clause
let toExpr (par:ParameterExpression) (clause: Clause) =
let constExpr (x:'a) =
Expression.Constant(x, typeof<'a>) |> asExpr
let callExpr (i:Expression) (m:String) (p:Expression) =
Expression.Call(i, m, Array.empty, p)
let arrExpr (xs:'a array) =
Expression.NewArrayInit(typeof<'a>, Array.map (constExpr >> asExpr) xs) |> asExpr
let inExpr (vEx:Expression) (arrEx:Expression) =
Expression.Call(typeof<Enumerable>, "Contains", [|vEx.Type|], arrEx, vEx) |> asExpr
let foldClause simpleF complexF clause =
let unitem = function
| Prop ss -> ss
let rec loop clause cont =
match clause with
| Simple (item, condition, value) ->
cont (simpleF (unitem item) condition value)
| Complex (l, op, r) ->
loop l (fun lval ->
loop r (fun rval ->
type LogOp =
| Or
| And
type Cond =
| Eq
| Neq
| Greater
| Less
| GreaterOrEqual