Skip to content

Instantly share code, notes, and snippets.

@jchia
jchia / mnist-lightning.py
Created May 6, 2022 17:06
PyTorch Lightning CLI not picking up docstring arg description to make help text
#!/bin/env python3
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import CyclicLR
fn magic_parser(i: &[u8]) -> IResult<&[u8], ()> {
ctor::value((), streaming::tag((0x7b5aff5f_u32).to_be_bytes()))(i)
}
fn get_magic(i: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
// `i` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirementrustcE0759
let x = magic_parser(i)?;
Ok(())
}
@jchia
jchia / gist:5ba9852c67c96ca68dbe414f559e11df
Last active December 27, 2021 14:22
How to fix bar2() concisely? (bar1 is not concise)
fn foo() -> Result<(), std::io::Error> {
Ok(())
}
// Compiles, but verbose (using map_err).
fn bar1() -> Result<(), Box<dyn std::error::Error>> {
foo().map_err(|e| e.into())
}
// Does not compile.
@jchia
jchia / gist:e9bb569ec0d311268b60346fe135ebb8
Created September 3, 2020 09:21
Redundant constraint: Eq (Element Prod)
import ClassyPrelude
newtype Prod = Prod ByteString
deriving newtype (Eq, Hashable, Ord, IsString, MonoFoldable, Monoid, Semigroup)
type instance Element Prod = Word8
• Redundant constraint: Eq (Element Prod)
• In an expression type signature:
Eq (Element Prod) => Element Prod -> Prod -> Bool
class Isomorphic a b where
convert :: a -> b
data Unit = Unit deriving Generic
data Three a b c = A a | B b | C c deriving Generic
data Foo a b = Foo a b deriving Generic
data MyBool = MyFalse | MyTrue deriving Generic
@jchia
jchia / gist:c50b638fa9ee8f6b4e7d51aef5771c0f
Last active August 27, 2020 06:12
No instance for (Biplate C3 Char) arising from a use of ‘dbg4’
import Data.Data (Data)
import Data.Generics.Uniplate.Operations
import Type.Reflection (Typeable)
-- The haddock for Biplate (http://hackage.haskell.org/package/uniplate-1.6.12/docs/Data-Generics-Uniplate-Operations.html) says this:
-- instance (Data a, Data b, Uniplate b) => Biplate a b
-- But why is there an error on the last line that there is no instance for (Biplate C3 Char)?
dbg1 :: Data a => a -> Bool
dbg1 = const True
{-# LANGUAGE TemplateHaskell #-}
module Main where
import ClassyPrelude
import Data.Profunctor
import Data.Profunctor.Product
import Data.Profunctor.Product.Default
import Data.Profunctor.Product.TH
8.2.2
=====
name: array
version: 0.5.2.0
id: array-0.5.2.0
key: array-0.5.2.0
license: BSD3
maintainer: libraries@haskell.org
synopsis: Mutable and immutable arrays
description:
@jchia
jchia / gist:6cc92e686dda4a422832dcb191a2c2b5
Created April 22, 2020 13:32
GHC redundant constraint
newtype Sym = Sym { unSym :: ByteString }
deriving ( Eq, Generic, Hashable, Ord, IsString, MonoFoldable, Monoid, Semigroup, C.FromField, C.ToField )
type instance Element Sym = Word8
GHC 8.8 Warning:
================
• Redundant constraint: Eq (Element Sym)
• In an expression type signature:
Eq (Element Sym) => Element Sym -> Sym -> Bool
data Session = Session { date :: Date, nightDay :: NightDay } deriving (Bounded, Eq, Generic, Ord)
instance Show Session where
show (Session date Night) = show date `snoc` 'n'
show (Session date Day) = show date
sessionParser :: (Stream a, Token a ~ Char) => Parsec () a Session
sessionParser = do
date <- dateParser
night <- optional (char 'n')