我有以下数据类型(如列表):
data List a =
Nil
| Cons a (List a)
deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)
并创建了一个实例Monoid:
instance Monoid (List a) where
mempty = Nil
mappend Nil ys = ys
mappend (Cons x xs) ys = Cons x (mappend xs ys)
Run Code Online (Sandbox Code Playgroud)
我尝试编写一个实例来Arbitrary测试后者,但无法完成它:
instance Arbitrary a => Arbitrary (List a) where
arbitrary = do
Run Code Online (Sandbox Code Playgroud)
请帮助我完成该功能。
我有以下类型定义:
data Hello a b = Hi a
| Sali b
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
它不是一个实例,Applicative但我仍然可以用pure它来转换Applicative为什么?
*ExercisesTraversable Data.Monoid Control.Applicative> :t pure $ Hi 34
pure $ Hi 34 :: (Num a, Applicative f) => f (Hello a b)
*ExercisesTraversable Data.Monoid Control.Applicative> pure $ Hi 34
Hi 34
Run Code Online (Sandbox Code Playgroud)
当我尝试:
*ExercisesTraversable Data.Monoid Control.Applicative> (Sali (*2)) <*> (Sali 4)
<interactive>:23:1: error:
* Non type-variable argument
in the constraint: Applicative (Hello a)
(Use FlexibleContexts to permit this)
* When …Run Code Online (Sandbox Code Playgroud) 我想使用应用函数并尝试如下:
*ReaderExercise Control.Applicative> (+4) <*> (+3)
Run Code Online (Sandbox Code Playgroud)
然后收到以下错误消息:
<interactive>:51:11: error:
* Occurs check: cannot construct the infinite type: a ~ a -> b
Expected type: (a -> b) -> a
Actual type: a -> a
* In the second argument of `(<*>)', namely `(+ 3)'
In the expression: (+ 4) <*> (+ 3)
In an equation for `it': it = (+ 4) <*> (+ 3)
* Relevant bindings include
it :: (a -> b) -> b (bound at <interactive>:51:1)
Run Code Online (Sandbox Code Playgroud)
我期望的是一个带有一个参数的返回函数。
无限类型是什么意思?
我从haskellbook中有以下代码片段,它逐步显示monad变换器将如何解包:
module OuterInner where
import Control.Monad.Trans.Except
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader
-- We only need to use return once
-- because it's one big Monad
embedded :: MaybeT (ExceptT String (ReaderT () IO)) Int
embedded = return 1
maybeUnwrap :: ExceptT String (ReaderT () IO) (Maybe Int)
maybeUnwrap = runMaybeT embedded
-- Next
eitherUnwrap :: ReaderT () IO (Either String (Maybe Int))
eitherUnwrap = runExceptT maybeUnwrap
-- Lastly
readerUnwrap :: () -> IO (Either String (Maybe Int))
readerUnwrap = runReaderT eitherUnwrap …Run Code Online (Sandbox Code Playgroud) 在Haskell中,我可以将参数约束到类型类,例如:
Prelude> f x y = x * y
Prelude> :t f
f :: Num a => a -> a -> a
Run Code Online (Sandbox Code Playgroud)
我试图在Scala以下方面做同样的事情:
def sendMessage[A: Array[Byte], B: Array[Byte]](producer: Eval[KafkaProducer[A, B]])(pr: Eval[ProducerRecord[A, B]]) =
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨:
[error] /home/developer/Desktop/scala/PureProducer/src/main/scala/TheProducer.scala:39:20: Array[Byte] does not take type parameters
[error] def sendMessage[A: Array[Byte], B: Array[Byte]](producer: Eval[KafkaProducer[A, B]])(pr: Eval[ProducerRecord[A, B]]) =
Run Code Online (Sandbox Code Playgroud)
如何约束Scala中的类型参数?
我有以下编译器抱怨的代码:
val state: KTable[String, String] = builder
.table("BARY-PATH", Materialized.as("PATH-STORE"))
Run Code Online (Sandbox Code Playgroud)
错误信息:
[error] /home/developer/Desktop/microservices/paths-stream/src/main/scala/io/khinkali/PathsTopology.scala:23:8: overloaded method value table with alternatives:
[error] [K, V](x$1: String, x$2: org.apache.kafka.streams.kstream.Materialized[K,V,org.apache.kafka.streams.state.KeyValueStore[org.apache.kafka.common.utils.Bytes,Array[Byte]]])org.apache.kafka.streams.kstream.KTable[K,V] <and>
[error] [K, V](x$1: String, x$2: org.apache.kafka.streams.Consumed[K,V])org.apache.kafka.streams.kstream.KTable[K,V]
[error] cannot be applied to (String, org.apache.kafka.streams.kstream.Materialized[Nothing,Nothing,Nothing])
[error] .table("BARY-PATH", Materialized.as("PATH-STORE"))
[error] ^
Run Code Online (Sandbox Code Playgroud)
然后我尝试:
val state: KTable[String, String] = builder
.table[String, String]("BARY-PATH", Materialized[String, String,KeyValueStore[org.apache.kafka.common.utils.Bytes, Array[Byte]]].as("PATH-STORE"))
Run Code Online (Sandbox Code Playgroud)
编译器仍然抱怨:
[error] /home/developer/Desktop/microservices/paths-stream/src/main/scala/io/khinkali/PathsTopology.scala:24:43: object org.apache.kafka.streams.kstream.Materialized is not a value
[error] .table[String, String]("BARY-PATH", Materialized[String, String,KeyValueStore[org.apache.kafka.common.utils.Bytes, Array[Byte]]].as("PATH-STORE"))
Run Code Online (Sandbox Code Playgroud)
我阅读了API 文档,但无法弄清楚,我做错了什么?
方法实现:
* Materialize a {@link StateStore} …Run Code Online (Sandbox Code Playgroud) 我有以下函数,它做递归:
@tailrec
private def pool[F[_]: Monad, A]
: Consumer[String, String] => (Vector[KkConsumerRecord] => F[A]) => IO[Unit]
= consumer => cb => {
val records: ConsumerRecords[String, String] = consumer.poll(Long.MaxValue)
val converted = records.iterator().asScala.map(rec => {
KkConsumerRecord(rec.key(), rec.value(), rec.offset(), rec.partition(), rec.topic())
})
val vec = converted.foldLeft(Vector.empty[KkConsumerRecord]) { (b, a) =>
a +: b
}
cb(vec)
pool(consumer)(cb)
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨:
[error] /home/developer/Desktop/microservices/bary/kafka-api/src/main/scala/io/khinkali/Consumer/KkConsumer.scala:57:10: type mismatch;
[error] found : org.apache.kafka.clients.consumer.Consumer[String,String]
[error] required: cats.Monad[?]
[error] pool(consumer)(cb)
[error] ^
[error] two errors found
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我想在SAP Web IDE上的ES6中编写代码并按照指令https://blogs.sap.com/2017/11/01/next-generation-javascript-in-ui5-using-sap-web-ide/。

但是如您所见,我仍然收到错误消息。
我究竟做错了什么?
我正在使用Webpack4来构建我的React应用程序,其webpack.config.js外观如下:
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require('path');
module.exports = {
entry: ['@babel/polyfill', './src/index.js'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"]
}
}
},
{
test: /\.html$/,
use: [{
loader: "html-loader",
options: {
minimize: true
}
}]
},
{
test: /\.scss$/,
use: [{
loader: MiniCssExtractPlugin.loader
}, {
loader: "style-loader"
}, { …Run Code Online (Sandbox Code Playgroud) 我尝试使用如下粘性属性:
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>Cockpit</title>
<style>
.topbar-container {
width: 100%;
height: 100px;
position: sticky;
background-color: #2d3e50;
z-index: 999; }
.navi-container {
width: 100%;
height: 50px;
background-color: #119C7E;
position: sticky;
z-index: 999; }
.portfolio-container {
width: 992px;
position: absolute;
top: 130px; }
.app-container {
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
top: 0px;
position: relative;
align-items: center; }
* {
padding: 0;
margin: 0; }
html,
body {
height: 100%; …Run Code Online (Sandbox Code Playgroud)haskell ×5
scala ×3
apache-kafka ×2
css ×1
html ×1
java ×1
javascript ×1
reactjs ×1
sap-web-ide ×1
scala-cats ×1
webpack ×1