Skip to content

Instantly share code, notes, and snippets.

(*
* BatEnum - Enumeration over abstract collection of elements.
* Copyright (C) 2003 Nicolas Cannasse
* 2009 David Rajchenbach-Teller, LIFO, Universite d'Orleans
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
@klapaucius
klapaucius / gist:1319152
Created October 27, 2011 09:25
map in ocaml
(* batList.ml *)
type 'a mut_list = {
hd: 'a;
mutable tl: 'a list
}
external inj : 'a mut_list -> 'a list = "%identity"
let map f = function
@klapaucius
klapaucius / gist:1319154
Created October 27, 2011 09:27
map in haskell
-- GHC/Base.lhs
map _ [] = []
map f (x:xs) = f x : map f xs
@klapaucius
klapaucius / gist:1319163
Created October 27, 2011 09:31
in-place quick sort
import Data.Vector.Generic
import qualified Data.Vector.Generic.Mutable as M
iqsort :: (Vector v a, Ord a) => v a -> v a
iqsort = modify go where
go xs | len < 2 = return ()
| otherwise = do
p <- xs `M.read` (len `div` 2)
m <- M.unstablePartition (< p) xs
go $ M.slice 0 m xs
$s$wfoldlM'_loop_s1qP =
\ (sc_s1qv :: GHC.Prim.Int#)
(sc1_s1qw :: GHC.Prim.Int#)
(sc2_s1qx :: GHC.Prim.Double#) ->
case GHC.Prim.>=# sc1_s1qw ipv1_s1a1 of _ {
GHC.Bool.False ->
case GHC.Prim.>=# sc_s1qv ipv4_s1fy of _ {
GHC.Bool.False ->
$s$wfoldlM'_loop_s1qP
(GHC.Prim.+# sc_s1qv 1)
@klapaucius
klapaucius / post.md
Created November 17, 2011 18:43
! vs unsafeIndex

Ответ на вопрос в этом посте.

Для начала слегка модифицируем код, чтоб он компилировался.

import qualified Data.List.Stream as S
import qualified Data.Vector.Unboxed as U

f n = let arr = U.enumFromTo 1 n 
      in S.sum $ S.map (\i -> arr U.! (i `rem` n)) $ S.unfoldr (\i -> if i < n then Just (i, i+1) else Nothing) 0
@klapaucius
klapaucius / gist:1393019
Created November 25, 2011 07:56
true-signature
{-# LANGUAGE ScopedTypeVariables #-}
foo :: forall a. Eq a => [a] -> Bool
foo (x:y:_) = x == y
foo [_] = foo ([] :: [a])
foo [] = False
class Functor f where
type C f a :: Constraint
type C f a = ()
fmap :: (C f a, C f b) => (a -> b) -> f a -> f b
-- Инстансы для "неограниченных" функторов остались без изменений:
instance Functor [] where
fmap = map
instance Functor Set where
foo :: Eq a => [a] -> Bool
foo (x:y:_) = x == y
foo [_] = foo ([] :: [a])
foo [] = False
@klapaucius
klapaucius / _post.md
Last active November 7, 2021 22:36
spine-strict_lists

Строгость и нищета списков.

Map и структурная рекурсия.

Применить функцию к каждому элементу списка - это просто:

GHC/Base.lhs

map _ []     = []
map f (x:xs) = f x : map f xs