我试图从泛型类型创建实例T
,例如,
class Factory {
public static generate<T>(): T {
return new T();
}
}
Run Code Online (Sandbox Code Playgroud)
但由于T
只是一种类型而不是构造函数,我们不能这样做.
是否无法在TypeScript中从泛型类型创建实例?
我也读过这些文章,但找不到解决方案.
正如我所知,无形提供了HList
(异类列表)类型,它可以包含多种类型.
可折叠HList
吗?例如,
// ref - Composable application architecture with reasonably priced monad
// code - https://github.com/stew/reasonably-priced/blob/master/src/main/scala/reasonable/App.scala
import scalaz.{Coproduct, Free, Id, NaturalTransformation}
def or[F[_], G[_], H[_]](f: F ~> H, g: G ~> H): ({type cp[?] = Coproduct[F,G,?]})#cp ~> H =
new NaturalTransformation[({type cp[?] = Coproduct[F,G,?]})#cp,H] {
def apply[A](fa: Coproduct[F,G,A]): H[A] = fa.run match {
case -\/(ff) ? f(ff)
case \/-(gg) ? g(gg)
}
}
type Language0[A] = Coproduct[InteractOp, AuthOp, A]
type Language[A] = Coproduct[LogOp, Language0, …
Run Code Online (Sandbox Code Playgroud) 我被告知Scala提供了UNTIL, REPEAT
控制流模型的功能.
在研究能力时,我找到了代码:
// ref: https://gist.github.com/metasim/7503601
def REPEAT(body: => Unit) = new {
def UNTIL(condition: => Boolean): Unit = {
body
if (condition) ()
else UNTIL(condition)
}
}
// test code
REPEAT {
x = x + 1
} UNTIL (x > 3)
Run Code Online (Sandbox Code Playgroud)
为什么需要new
关键字REPEAT
功能?
因为Free
在Scalaz 7.1.5中不是monad实例,所以我不能使用定义的有用方法Applicative
,Apply
依此类推.
/* ref - http://tpolecat.github.io/assets/sbtb-slides.pdf */
import Free._, Coyoneda._
type ResultSetIO[A] = FreeC[ResultSetOp, A]
val next : ResultSetIO[Boolean] = liftFC(Next)
def getString(index: Int): ResultSetIO[String] = liftFC(GetString(index))
def getInt(index: Int) : ResultSetIO[Int] = liftFC(GetInt(index))
def close : ResultSetIO[Unit] = liftFC(Close)
// compile errors
def getPerson1: ResultSetIO[Person] =
(getString(1) |@| getInt(2)) { Person(_, _)}
def getNextPerson: ResultSetIO[Person] =
next *> getPerson
def getPeople(n: Int): ResultSetIO[List[Person]] =
getNextPerson.replicateM(n) // List.fill(n)(getNextPerson).sequence
Run Code Online (Sandbox Code Playgroud)
erorr信息是,
Error:(88, 19) value |@| is …
Run Code Online (Sandbox Code Playgroud) 是否有可能为每个子类创建幺半群?例如,
package currency
final case class GBP[A: Monoid](amount: A)
object Implicits {
implicit class CurrencyOps[A: Monoid](a: A) {
def GBP = currency.GBP(a)
}
implicit def gbpMonoid[A: Monoid]: Monoid[GBP[A]] = new Monoid[GBP[A]] {
override def zero =
GBP(Monoid[A].zero)
override def append(f1: GBP[A], f2: => GBP[A]): GBP[A] =
GBP(Semigroup[A].append(f1.amount, f2.amount))
}
}
test("GBP support plus") {
1.GBP |+| 2.GBP shouldBe 3.GBP // passed
}
Run Code Online (Sandbox Code Playgroud)
我想补充的代表更多的案例类的货币(例如USD
,EUR
..)
sealed trait Currency
final case class GBP[A: Monoid](amount: A) extends Currency …
Run Code Online (Sandbox Code Playgroud) 我正在阅读The Complete Idiot的Common Lisp包指南,我已经创建了一个名为named BOB
using 的新包make-package
.但是之后(in-package :bob)
,我无法使用任何符号,而这些符号在" CL-USER
包装"中可以使用.例如:
CL-USER> (make-package :bob)
CL-USER> (in-package :bob)
BOB> (+ 1 2)
;; caught COMMON-LISP:STYLE-WARNING:
;; undefined function +
Run Code Online (Sandbox Code Playgroud)
我也尝试使用CL-USER
包,如下所示,但我得到了相同的结果:
CL-USER> (make-package :bob :use '(:cl-user))
Run Code Online (Sandbox Code Playgroud)
我如何使用中定义的符号CL-USER
?
我在读斯卡拉学校.根据指南,Map可以包含像
def adder(x: Int, y:Int): Int = x + y
def add2(x:Int):Int = adder(2, x:Int)
val add3 = adder(3, _:Int)
val map = Map(
"adder" -> { adder(_, _) },
"add2" -> { add2(_) },
"add3" -> { add3(_) }
)
Run Code Online (Sandbox Code Playgroud)
好.上面的代码是编译的.但是如何调用地图中保存的功能?这些代码不起作用
map.get("adder")(2, 3) // compile error
val adderFunc: (Int, Int) => Int = map.get("adder") // compile error
Run Code Online (Sandbox Code Playgroud)
Scala编译器提供此错误消息
[error] /home/user/scalaExample/src/test/scala/CollectionBasicsTest.scala:61: Option[Object] does not take parameters
[error] map.get("adder")(2, 3) // compile error
[error] ^
[error] /home/user/scalaExample/src/test/scala/CollectionBasicsTest.scala:62: type mismatch; …
Run Code Online (Sandbox Code Playgroud) 我试图使用lisp代码制作可执行文件.但是我根本无法编译 lisp文件,因为hellowolrd
在加载helloworld
系统 之前没有包
;; test.lisp
(asdf:load-system :helloworld)
(defun main()
(helloworld:start))
Run Code Online (Sandbox Code Playgroud)
当然,我制作了helloworld
系统并将其放入~/quicklisp/local-projects/
.helloworld
系统成功加载没有错误.
;; ~/quicklisp/local-projects/helloworld/helloworld.asd
(asdf:defsystem helloworld
:version "1.0"
:components ((:file "package")))
;; ~/quicklisp/local-projects/helloworld/package.lisp
(defpackage :helloworld
(:use :common-lisp :asdf)
(:export :start))
(in-package :helloworld)
(defun start()
(format t "Welcome, ASDF"))
Run Code Online (Sandbox Code Playgroud)
我想编译test.lisp
没有显式加载.我也试过use-package
和defpackage
,但失败了.
;; test.lisp
(asdf:load-system :helloworld)
(use-package :helloworld)
(defun main()
(helloworld:start))
;; test.lisp
(asdf:load-system :helloworld)
(defpackage :test
(:use :cl :asdf)
(:export :main))
(in-package :test)
(defun main()
(helloworld:start)) …
Run Code Online (Sandbox Code Playgroud) 我很难转换简单的CPS功能
这是CPS风格的方形功能
-- from : http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style
square :: Int -> Int
square x = x * x
square_cps :: Int -> ((Int -> r) -> r)
square_cps = \cont -> cont (square x)
-- square_cps 3 print will write '9' out in console
Run Code Online (Sandbox Code Playgroud)
现在,我想以相反的顺序更改函数参数
square_cps' :: ((Int -> r) -> r) -> Int
square_cps' = ?
Run Code Online (Sandbox Code Playgroud)
这不可能吗?
我正在使用Emacs.
有没有办法在函数上添加钩子?
假设有降价输出功能.
它旨在将HTML文件导出到当前工作'markdown file'的当前目录中.
但是,我想将HTML文件导出到另一个目录中.如何在没有修改Emacs markdown插件(markdown-mode.el
)的情况下执行此操作?
这是markdown-mode.el
出口功能:
(defun markdown-export (&optional output-file)
"Run Markdown on the current buffer, save to file, and return the filename.
If OUTPUT-FILE is given, use that as the filename. Otherwise, use the filename
generated by `markdown-export-file-name', which will be constructed using the
current filename, but with the extension removed and replaced with .html."
(interactive)
(unless output-file
(setq output-file (markdown-export-file-name ".html")))
(when output-file
(let* ((init-buf (current-buffer))
(init-point (point))
(init-buf-string (buffer-string))
(output-buffer (find-file-noselect …
Run Code Online (Sandbox Code Playgroud) 我有点像 emacs 菜鸟。
tomorrow-night-bright-theme
除了下面的荧光灯我完全满意
我读过一些文章,解释了如何在 Emacs 中更改字体。
1. 在 emacs 中更改高亮线颜色
2. 更改 Emacs 语法高亮颜色
但是,我不知道与该颜色相关的符号是什么。
我读过eval-after-load和add-hook.根据页面,代码输入eval-after-load
执行一次.
另一方面,add-hook
每次打开该模式的缓冲区时,内部代码都会运行.
好.但是,如何确定哪些代码应该在eval-after-load
块或add-hook
块内?例如,
(eval-after-load "js2-mode"
'(progn
(js2-basic-offset 2)))
Run Code Online (Sandbox Code Playgroud)
要么
(eval-after-load "js2-mode"
'(progn
(defun custom:js2-config ()
(js2-basic-offset 2))
(add-hook 'js2-mode-hook 'custom:js2-config)))
Run Code Online (Sandbox Code Playgroud)
我不是在问js2-basic-offset
.
有一般规则吗?或..每当我使用时eval-after-load
,我都要问?
scala ×5
emacs ×3
lisp ×3
scalaz ×3
common-lisp ×2
elisp ×2
shapeless ×2
asdf ×1
collections ×1
emacs-faces ×1
generics ×1
haskell ×1
hook ×1
major-mode ×1
map ×1
monads ×1
quicklisp ×1
sbcl ×1
themes ×1
typescript ×1