小编dfe*_*uer的帖子

打印一行中每个单词的首字母

我搜索了其他帖子,但没有找到适合我需求的答案。我有一个以空格分隔的文件。我想打印给定行中每个单词的第一个字母。例如:

cat test.txt
This is a test sentence.
Run Code Online (Sandbox Code Playgroud)

使用 sed、awk 或组合,我希望输出为“Tiats”。关于为我指明正确方向的任何建议?

bash awk sed

4
推荐指数
2
解决办法
5927
查看次数

如何将此过程代码设计为基于类(面向对象)?

我是初学者 - 中级自学Python开发人员,

在我完成的大多数项目中,我可以看到以下过程重复.我没有任何外部家庭代码经验,我认为下面的代码不是那么专业,因为它不可重复使用,似乎它不适合容器中的所有功能,而是在不同模块上的松散耦合功能.

def get_query():
    # returns the query string
    pass

def make_request(query):
    # makes and returns the request with query
    pass

def make_api_call(request):
    # calls the api and returns response
    pass

def process_response(response):
    # process the response and returns the details
    pass

def populate_database(details):
    # populates the database with the details and returns the status of population
    pass

def log_status(status):
    # logs the status so that developer knows whats happening
    pass

query = get_query()
request = make_request(query)
response …
Run Code Online (Sandbox Code Playgroud)

python oop class-design

4
推荐指数
3
解决办法
849
查看次数

在Haskell中将函数作为参数传递给不同类型

有没有办法将作为参数传递的函数应用于两种不同的类型?作为一个人为的例子,我可以(Maybe Int, Maybe Bool)用表达式创建一个(Just 3, Just True),但是如果我尝试使这个行为更通用的功能

generic :: (a -> Maybe a) -> (Maybe Int, Maybe Bool)
generic f = (f 3, f True)
Run Code Online (Sandbox Code Playgroud)

所以我可以做类似的事情generic Just,编译器抱怨因为类型变量a是常量.

其用例是将通用函数应用于树结构,其中每个节点按类型进行参数化.

haskell parametric-polymorphism

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

假设单位类型是否相等是否安全?

假设所有类型()都相同是否安全?也就是说,可以使用以下方法来打破类型安全吗?

-- Bad postulate
unitsEqual :: (x :: ()) :~: (y :: ())
unitsEqual = unsafeCoerce (Refl :: '() :~: '())
Run Code Online (Sandbox Code Playgroud)

haskell types

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

索引向量中的类型参数的顺序

在依赖类型编程中定义似乎是常规的

data Vec :: Type -> Nat -> Type where
  Nil :: Vec a 'Z
  Cons :: a -> Vec a n -> Vec a ('S n)
Run Code Online (Sandbox Code Playgroud)

在Haskell,但是,Functor,Applicative,Foldable,Traversable,Eq1,Ord1,等,班似乎有一个良好的情况下,周边的翻转参数,来Vec :: Nat -> Type -> Type.

通常惯例有一些重要原因吗?或者它恰好是人们碰巧在不基于类型类的语言中使用的?

haskell dependent-type

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

努力实现Monad功能

最近,我正在玩Haskell monad并试图了解这个概念.

假设声明了一个可以有多个子树的树数据类型.

data MyTree a = MyTree a [MyTree a]
Run Code Online (Sandbox Code Playgroud)

而且我正在尝试实现一个函数,如果树在树中包含任何"Nothing"值,则返回"Nothing".否则,提取所有m值并返回一个包装树.

因此函数类型签名具有以下内容.

check :: Monad m => MyTree (m a) -> m (MyTree a)
Run Code Online (Sandbox Code Playgroud)

这是我目前的实施.

check (MyTree v []) = v >>= (\v' -> return (MyTree v' []))
check (MyTree v (x:xs)) =
  v >>= (\v' -> check x >>= (\t' -> return (MyTree v' [t'])))
Run Code Online (Sandbox Code Playgroud)

我在v上使用绑定运算符,以便我可以得到它的纯值.然后我用列表中的头部值递归调用"check"函数.最后,我将最终结果包装好.

我测试了一些样品并得到了以下结果.

> test1 = MyTree (Just 1) [MyTree (Just 2) [MyTree (Just 3) []]]
> check test1
Just (MyTree 1 [MyTree 2 …
Run Code Online (Sandbox Code Playgroud)

monads haskell functional-programming

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

如何反转haskell中的图形?

对于练习我需要反转图形(反转所有边缘),但我没有得到任何结果.所以我需要一些帮助.

我知道你可能不想为我解决这个问题,所以这不是我要求的.我只需要一些建议......

所以要达到它:

data Graph a = G
  { nodes :: [a]
  , successors :: a -> [a] }

reverseGraph :: Eq a => Graph a -> Graph a
Run Code Online (Sandbox Code Playgroud)

图表必须包含以下参数:节点列表和定义后继者的函数.此函数具有以下类型: a -> [a]

例如:

graph1 :: Graph Int
graph1 = G [1..6] $ \case   1 -> [2,3]
                            2 -> []
                            3 -> [1,4,6]
                            4 -> [1]
                            5 -> [3,5]
                            6 -> [2,4,5]
Run Code Online (Sandbox Code Playgroud)

反转图表将是:

reverseGraph graph1 ~>
    2 -> [1,6]
    3 -> [1,5]
    1 -> [3,4]
    4 -> [3,6]
    6 -> …
Run Code Online (Sandbox Code Playgroud)

haskell graph

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

需要多少张fmap?

Daniel Wagner 的评论使我想到了这个问题。让我们从过度简化开始。假设你有一个类型

data Foo a = Foo [a]
Run Code Online (Sandbox Code Playgroud)

然后您可以编写Functor实例

instance Functor Foo where
  fmap f (Foo l) = Foo (fmap f l)
Run Code Online (Sandbox Code Playgroud)

您可以将右侧重写为

Foo . fmap f $ l
Run Code Online (Sandbox Code Playgroud)

认识到了(->) afmap = (.)你可以把它写

fmap Foo (fmap f) l
Run Code Online (Sandbox Code Playgroud)

重复,你得到

fmap (fmap Foo) fmap f l
Run Code Online (Sandbox Code Playgroud)

所以,最后,

fmap f (Foo l) =
  fmap fmap fmap Foo fmap f l
Run Code Online (Sandbox Code Playgroud)

如果选择一个稍微复杂一点的函子怎么办?

data Bar = Bar [Maybe a]

instance Functor Bar where
  fmap f …
Run Code Online (Sandbox Code Playgroud)

haskell

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

代表无限动作链的 Monad 转换器?

我试图实现一个表示无限动作链的 monad 转换器,如下所示:

import Control.Arrow
import Control.Monad
import Data.Functor.Classes
import Data.Functor.Identity
import Data.Monoid
import Data.Semigroup
import Text.Read

import qualified Control.Monad.Trans.Class as T

newtype WhileT m a = WhileT {uncons :: m (a, WhileT m a)}

instance T.MonadTrans WhileT where
    lift m = WhileT $ do
        x <- m
        pure (x, T.lift m)

headW :: Functor m => WhileT m a -> m a
headW (WhileT m) = fmap fst m

tailW :: Functor m => WhileT m a -> m …
Run Code Online (Sandbox Code Playgroud)

monads haskell while-loop monad-transformers

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

当Data.Vector.unfoldr没有融合时会发生什么?

假设我创建了一个Vector使用unfoldr,而不是unfoldrN,并且它没有融合,所以实际上需要创建向量.系统如何决定制作它的大小?我在文档中找不到任何相关内容.源代码显示它调用unstream,它有很多复杂的代码,我无法做出头或尾.

arrays haskell

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