我想定义一个记录器功能,例如
myPutStrLn = putStrLn . (++) "log: "
main = do myPutStrLn "hello"
Run Code Online (Sandbox Code Playgroud)
很好 现在,我想格式化与所提供的字符串printf,这样
myPutStrLn $ printf "test %d" (23 :: Int)
Run Code Online (Sandbox Code Playgroud)
大!由于我经常使用这种模式,因此我想将其printf纳入记录器功能:
myPrintf = logger . printf
where
-- note, this is just an example. should be
-- replaceable with any function with this
-- typesignature
logger :: String -> IO ()
logger = putStrLn . (++) "log: "
main = myPrintf "test %d" (23 :: Int)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这失败了
The function `myPrintf' is applied to …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写以下函数,以便可以将其与Text.Printf.printf的可变数量的参数一起使用。我尝试使用类型类,但找不到任何不逐个字符和逐个参数处理格式字符串的示例。我希望能够捕获所有参数并将其立即传递给 printf 。
-- panic :: PrintfType r -> Int -> String -> r
panic lvl fmt =
let fail :: String -> String -> a
fail s s' = error $ printf "%s: %s" s s'
in flip fail (show lvl) . printf fmt
Run Code Online (Sandbox Code Playgroud)
我想使用这样的函数:
panic 0 "Err" -- Exception: "Err: 0"
panic 1 "Err %s %d" "fatal" 42 -- Exception: "Err fatal 42: 1"
Run Code Online (Sandbox Code Playgroud)
我应该如何继续实施?
我正在尝试在Haskell中创建一个polyvariadic函数,我用这个答案来创建一个基本函数.这是函数的代码:
class SumRes r where
sumOf :: Integer -> r
instance SumRes Integer where
sumOf = id
instance (Integral a, SumRes r) => SumRes (a -> r) where
sumOf x = sumOf . (x +) . toInteger
Run Code Online (Sandbox Code Playgroud)
但问题是:在没有任何参数的情况下调用函数时,它不起作用.
Couldn't match expected type 'Integer' with actual type 'Integer -> r0'
Probable cause: 'sumOf' is applied to too few arguments
Run Code Online (Sandbox Code Playgroud)
例如,我希望能够编写sumOf :: Integer并返回此函数0.
我该怎么做?
假设我想在Go中为切片创建等效的JavaScriptArray.splice函数.我有以下代码:
func splice(slice []int, index, amount int, elements ...int) []int {
newslice := make([]int, 0)
for i := 0; i < index; i++ {
newslice = append(newslice, slice[i])
}
for i := index + amount; i < len(slice); i++ {
newslice = append(newslice, slice[i])
}
for _, el := range elements {
newslice = append(newslice, el)
}
return newslice
}
Run Code Online (Sandbox Code Playgroud)
此示例将起作用,但仅适用于类型的参数int.我想让它变得通用,我知道我应该给variadic参数elements类型interface{},但是如何从函数内部创建一个具有该接口类型的新切片?
换句话说,如何根据函数第一行中参数的类型动态指定切片的类型,在何处newslice创建?
在寻找Polyvariadic函数示例时,我发现了这个资源: StackOverflow:如何创建polyvariadic haskell函数?,有一个这样的答案片段:
class SumRes r where
sumOf :: Integer -> r
instance SumRes Integer where
sumOf = id
instance (Integral a, SumRes r) => SumRes (a -> r) where
sumOf x = sumOf . (x +) . toInteger
Run Code Online (Sandbox Code Playgroud)
然后我们可以使用:
*Main> sumOf 1 :: Integer
1
*Main> sumOf 1 4 7 10 :: Integer
22
*Main> sumOf 1 4 7 10 0 0 :: Integer
22
*Main> sumOf 1 4 7 10 2 5 8 22 :: Integer
59 …Run Code Online (Sandbox Code Playgroud) 我正在尝试解析日期,例如09/10/2015 17:20:52:
{-# LANGUAGE FlexibleContexts #-}
import Text.Parsec
import Text.Parsec.String
import Text.Read
import Control.Applicative hiding (many, (<|>))
data Day = Day
{ mo :: Int
, dy :: Int
, yr :: Int
} deriving (Show)
data Time = Time
{ hr :: Int
, min :: Int
, sec :: Int
} deriving (Show)
day = listUncurry Day <$> (sepCount 3 (char '/') $ read <$> many digit)
time = listUncurry Time <$> (sepCount 3 (char ':') …Run Code Online (Sandbox Code Playgroud) polyvariadic ×6
haskell ×5
printf ×2
applicative ×1
ghc ×1
go ×1
parsec ×1
recursion ×1
slice ×1
typeclass ×1