转置嵌套数据结构 工作代码:示例:

如何解决转置嵌套数据结构 工作代码:示例:

我需要一个像 Vector [Vector a] -> Vector [Vector a] 这样的函数,其中所有内容都保持在同一个列表索引内,但是(对于每个列表索引)第 j 个内部向量的第 i 个元素成为第 j 个元素第 i 个内部向量的元素。 我怀疑我做得比它需要的要困难得多。

我想我特别想使用 this implementation of fixed-length vectors,但我可以考虑其他选项。

为了简单起见,我在一个替代的 Pair 类上工作,但我的实现在很大程度上依赖于对 Pair 结构的模式匹配,我认为不会的不适合Vec n

Pair 类几乎不相关;它类似于 Vec nat2,但我只实现了我认为需要的内容:

data Pair a where
  Pair :: a -> a -> Pair a
  deriving (Read,Show,Eq)
instance Functor Pair where
  fmap f (Pair a1 a2) = Pair (f a1) (f a2)
instance Applicative Pair where
  pure a = Pair a a
  (Pair f1 f2) <*> (Pair a1 a2) = Pair (f1 a1) (f2 a2)
instance Foldable Pair where
  foldr f b (Pair a1 a2) = a1 `f` (a2 `f` b)

(!) :: Pair a -> Bool -> a
(Pair a1 a2) ! b = if b then a2 else a1

universe :: Pair Bool
universe = Pair False True

工作代码:

transpose :: Pair [Pair a] -> Pair [Pair a]
transpose (Pair b1s b2s) = t' b1s b2s
  where t' ((Pair a11 a12) : a1s) ((Pair a21 a22) : a2s)
            = Pair ((Pair a11 a21) :) ((Pair a12 a22) :) <*> t' a1s a2s
        t' [] [] = pure []
        -- A runtime error here is ok; I have external guarantees that this won't happen.
        t' _ _ = error "Transposition requires the lists to be of equal length."

示例:

> let subject = Pair [
      Pair (1,1,1) (1,2),Pair (1,2,3,2)
  ] [
      Pair (2,1) (2,Pair (2,2)
  ]
> transpose subject
↠ Pair [
      Pair (1,1),1)
  ] [
      Pair (1,2) (2,2)
  ]

经过一番努力,我可以几乎获得一个专门使用列表/函子/应用程序/可索引运算符而不是模式匹配的实现(因此应该适用于 Vec n),但我总是卡住。

如何重写我的 transpose 函数以处理其他一些结构 Pair,但具有参数大小? >

解决方法

我想我会使用 ZipList 和一个新的类似 ZipVector 来实现这一点。我们将从更标准的 transpose 开始:

transposeVV :: Vector (Vector a) -> Vector (Vector a)
transposeVV = getZipVector . traverse Finite

为了能够使用它,我们需要能够将两个 Vector 放在一起;我们将通过另外两个互为逆的转置来做到这一点:

transposeVL :: Vector [a] -> [Vector a]
transposeVL = getZipList . traverse ZipList

transposeLV :: [Vector a] -> Vector [a]
transposeLV = getZipVector . traverse Finite

你现在想要的是这些的结合:

transposeVlV :: Vector [Vector a] -> Vector [Vector a]
transposeVlV = transposeLV . fmap transposeVV . transposeVL

好吧,我把一些功能隐藏在 ZipVector 后面。那看起来像什么?这很容易;然而,与 ZipList 相比的一个转折是我们不能轻易构造一个无限向量。没什么大不了的,我们会假装的。 (很明显,你的长度索引向量不需要使用这个技巧。)

data ZipVector a
    = Inf a
    | Finite (Vector a)
    deriving Functor

getZipVector :: ZipVector a -> Vector a
getZipVector (Inf a) = V.singleton a -- hmmmm...
getZipVector (Finite as) = as

instance Applicative ZipVector where
    pure = Inf
    Inf f <*> Inf x = Inf (f x)
    Inf f <*> Finite xs = Finite (f <$> xs)
    Finite fs <*> Inf x = Finite (($x) <$> fs)
    Finite fs <*> Finite xs = Finite (V.zipWith ($) fs xs)

我们现在可以试一试。首先让我们定义您的 subject

subject :: Vector [Vector (Int,Int,Int)]
subject =
    V.generate 2 $ \i ->
        flip map [0..2] $ \j ->
            V.generate 2 $ \k ->
                (i+1,j+1,k+1)

然后,在 ghci 中:

> transposeVlV subject
[[[(1,1,1),(2,1)],[(1,2,3,1)]],[[(1,2),2)],2)]]]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res