小编Kev*_*ith的帖子

A => Class [A]使用ClassTag?

看看ClassTag#runtimeClass,它有一个返回类型Class[_],即Class根据我的理解,带有一个通配符参数.

我试图实现一个方法A => ClassTag[A]:

import scala.reflect._

scala> def f[A](x: A)(implicit ev: ClassTag[A]) = ev.runtimeClass
f: [A](x: A)(implicit ev: scala.reflect.ClassTag[A])Class[_]
Run Code Online (Sandbox Code Playgroud)

但是,def正如文档所示,该定义的输出是Class[_].

是否有可能改变f它的返回类型Class[A]?如果没有,那为什么不可能呢?

reflection scala

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

了解State Monad的`get`

鉴于:

*Main> let s = state $ \x -> ("foo", x)
*Main> :t s
s :: MonadState s m => m [Char]
Run Code Online (Sandbox Code Playgroud)

我试着打电话get s,但得到了以下编译时错误:

*Main> :t get
get :: MonadState s m => m s

*Main> let x = get s

<interactive>:95:5: error:
    • Non type-variable argument
        in the constraint: MonadState t ((->) (m [Char]))
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        x :: forall s (m :: * -> *) t.
             (MonadState s …
Run Code Online (Sandbox Code Playgroud)

haskell

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

在StateT中访问'a'

我正在尝试编写一个函数,StateT只是为了更多地了解它.

f,我想访问Int最后一个类型的参数StateT [Int] IO Int:

f :: StateT [Int] IO Int
f = state $ \xs -> update (error "I want a") xs

update :: Int -> [Int] -> (Int, [Int])      
update x []     = (x, [])
update x (y:ys) = (x+y, ys) 
Run Code Online (Sandbox Code Playgroud)

以下是我想称之为:

let x = return 55 :: StateT [Int] IO Int

参考runStateT:

*Main> :t runStateT
runStateT :: StateT s m a -> s -> m (a, s) …
Run Code Online (Sandbox Code Playgroud)

haskell

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

ReasonReact编译时错误的`self.send`

鉴于以下内容:

$cat src/Greeting.re
let component = ReasonReact.reducerComponent("Greeting");

type action =
 | Click;

type state = {
    count: int
};

let make = (_children) => {
  ...component,
  initialState: () => {count: 0},
  reducer: (action, state) =>
    ReasonReact.Update({count: state.count + 1}),
  render: (self) => {
     let message = "Clicked " ++ string_of_int(self.state.count) ++ "x";
        <div>
          <button
            onClick={_event => self.send(Click)}
          />
          {ReasonReact.stringToElement(message)}
        </div>
  }
};
Run Code Online (Sandbox Code Playgroud)

我得到以下编译时错误:

  17 ? <div>
  18 ?   <button
  19 ?     onClick={_event => self.send(Click)}
  20 ?   />
  21 ?   {ReasonReact.stringToElement(message)} …
Run Code Online (Sandbox Code Playgroud)

reason reason-react

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

明确声明Int列表?

我如何能明确标记的类型let myList = [1, 2, 3];list of int's

let xs: 'int list = [1,2,3];通过Try ReasonML尝试失败了.

ocaml reason

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

2 FoldLefts是平等的

为什么以下两个foldLeft产生相同的输出?

#1

scala> List(1,2,3).foldLeft(List[Int]())( (acc, el) => acc :+ el)
res114: List[Int] = List(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

并且,现在使用_ :+ _作为(B, A) => B参数.

#2

scala> List(1,2,3).foldLeft(List[Int]())(_ :+ _)
res115: List[Int] = List(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

特别是,accumulator在第二种情况下缺乏明确附加的内容使我感到困惑.

scala

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

过滤元组列表

我正在尝试过滤list of 2-tuples第一个元组值等于的位置0:

ghci> ys
[(0,55),(1,100)]

ghci> filter (\x -> x.fst == 0) ys
<interactive>:71:27:
    Couldn't match type `(Integer, Integer)' with `b0 -> c0'
    Expected type: [b0 -> c0]
      Actual type: [(Integer, Integer)]
    In the second argument of `filter', namely `ys'
    In the expression: filter (\ x -> x . fst == 0) ys
    In an equation for `it': it = filter (\ x -> x . fst == 0) ys
Run Code Online (Sandbox Code Playgroud)

我想要的输出:

[(1,100)]

我怎样才能做到这一点?另外,编译时错误是什么意思?

haskell

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

从CaseClass(Int,String)创建函数[Int,String,CaseClass] #tupled?

鉴于:

scala> case class Foo(x: Int, y: String)
defined class Foo
Run Code Online (Sandbox Code Playgroud)

我正在尝试Foo.tupled用来创建Function2[Int, String, Foo]:

scala> val fn2: Function2[Int, String, Foo] = Foo.tupled match { 
     |   case (param1, param2)  => { (param1, param2) => Foo(param1, param2) } 
     | }
<console>:18: error: constructor cannot be instantiated to expected type;
 found   : (T1, T2)
 required: ((Int, String)) => Foo
         case (param1, param2)  => { (param1, param2) => Foo(param1, param2) }
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用.我该如何解决这个破碎的代码?

scala

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

列出中间列表中的#()

我可以通过使用申请获得该index = 2项目.List

scala> List(1,2,3).apply(2)
res3: Int = 3

scala> val x = List(1,2,3)
x: List[Int] = List(1, 2, 3)

scala> x(2)
res4: Int = 3

scala> List(1,2,3).apply(2)
res5: Int = 3
Run Code Online (Sandbox Code Playgroud)

但是,为什么我不能做以下事情呢?

scala> List(1,2,3).(2)
<console>:1: error: identifier expected but '(' found.
       List(1,2,3).(2)
                   ^
Run Code Online (Sandbox Code Playgroud)

scala

-2
推荐指数
1
解决办法
67
查看次数

模式匹配/检查Function1的Arity

给出以下方法:

scala> def f: List[Any] => Any = xs => 1234 // this output does not matter
f: List[Any] => Any
Run Code Online (Sandbox Code Playgroud)

模式匹配是否可能List[Any] => Any?我没有unapplyFunction1上看到一个方法,所以我认为答案是否定的.

这是我正在尝试做的事情:

例:

def foo(x: Any) = x match { 
   case ... // to handle the case of List[Any] => Any]?
   case ...
}
Run Code Online (Sandbox Code Playgroud)

也许我可以计算出arityx: Any区分List[Any] => Any与其他一切(_)?

编辑:

我希望我不必依赖f.toString== <function1>.

scala

-2
推荐指数
1
解决办法
126
查看次数

标签 统计

scala ×5

haskell ×3

reason ×2

ocaml ×1

reason-react ×1

reflection ×1