标签: polyvariadic

Text.Printf.printf实现功能组合

我想定义一个记录器功能,例如

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)

printf haskell ghc polyvariadic

5
推荐指数
1
解决办法
285
查看次数

如何创建一个本身使用另一个多变量函数(例如 printf)的多变量函数?

我正在尝试编写以下函数,以便可以将其与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)

我应该如何继续实施?

printf haskell polyvariadic

5
推荐指数
1
解决办法
107
查看次数

Haskell polyvariadic函数没有参数

我正在尝试在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.

我该怎么做?

haskell functional-programming polyvariadic

4
推荐指数
1
解决办法
180
查看次数

Go中的变量泛型参数

假设我想在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创建?

go slice polyvariadic

3
推荐指数
2
解决办法
451
查看次数

Haskell,多变量函数和类型推断

在寻找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)

recursion haskell typeclass polyvariadic

3
推荐指数
1
解决办法
192
查看次数

如何将此解析器转换为应用程序的多变量解析器?

我正在尝试解析日期,例如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)

haskell parsec parser-combinators applicative polyvariadic

1
推荐指数
1
解决办法
136
查看次数