6

How to transform a seq into a tree

 3 years ago
source link: https://www.codesd.com/item/how-to-transform-a-seq-into-a-tree.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to transform a seq into a tree

advertisements

I have a seq of maps such as the coll below. I want to arrange it in a tree. Each map has a key named :parent which is the :id of the parent. Any hints on how can I do it ?

(def coll [{:id 1}
          {:id 2 :parent 1}
          {:id 3 :parent 1}
          {:id 4 :parent 2}
          {:id 5 :parent 4}
          {:id 6 :parent 5}
          {:id 7 :parent 5}
          {:id 8 :parent 5}
          {:id 9 :parent 7}])


If it walks like a tree...

(require '[clojure.zip :as z])

(defn create-zipper [s]
  (let [g (group-by :parent s)]
    (z/zipper g #(map :id (g %)) nil (-> nil g first :id))))

(def t (create-zipper coll)) ; using the coll defined in the OP

(-> t z/root)
;=> 1

(-> t z/children)
;=> (2 3)

(-> t z/next z/children)
;=> (4)

Note that you may preserve the format of the original nodes (rather than just returning id numbers) by using #(g (% :id)) as the children and (first (g nil)) as the root.

You can use post-order traversal to build up another representation of the tree if desired.

Tags clojure

Related Articles

Spark: How to transform a Seq from RDD to RDD

I'm just starting in Spark & Scala I have a directory with multiple files in it I successfully load them using sc.wholeTextFiles(directory) Now I want to go one level up. I actually have a directory that contains sub directories that contain files. M

In python, how to transform the slash into an escape sequence

I would like to know how to transform a string containing a Unicode ASCII descriptor into an actual Unicode character. For example, how can I transform u"\\u1234" to u"\u1234" ? Thank you.decode('unicode-escape'), for example: >>

How to transform a string into a python list?

l = "['Hello', 'my', 'name', 'is', 'Apple']" l1 = ['Hello', 'my', 'name', 'is', 'Apple'] type(l) returns str but I want it to be a list, as l1 is. How can I transform that string into a common list?the ast module has a literal_eval that does wha

How to transform this find_by_sql into named_scope?

How can I possibly turn into named_scope? def self.hero_badge_awardees return User.find_by_sql("select users.*, awards.*, badges.badge_type from users, awards, badges where awards.user_id = users.id and badges.id = awards.badge_id and badges.badge_ty

How to transform the text into input?

How can I create something such as: - there is some text - I click on it - It transforms into an input that contains the text I clicked on.How about something like this HTML <div onclick="transform(this)">Some text here</div> Javascr

How to transform unclassified elements into xml to match an xsd: sequence order?

in contrast to the answered situation of equal named child elements I'm trying to convert the following: <Person> <Address>5</Address> <Firstname>1234567890</Firstname> <Lastname> <MaidenName>The BFG</MaidenNam

How to transform vertical data into horizontal data with SQL?

I have a table "Item" with a number of related items, like so: ID Rel_ID Name RelRank --- ------ ---- ------- 1 1 foo 1 2 1 bar 2 3 1 zam 3 4 2 foo2 1 I'm trying to get a query so items with the same Rel_ID would appear in the same row, like so:

How to transform this Func into an expression?

I am playing around with expression trees and trying to better understand how they work. I wrote some sample code that I'm working with and hopefully someone can help me out. So I have this somewhat messy query: /// <summary> /// Retrieves the total

How to transform this string into a table?

Possible Duplicate: PHP - split String in Key/Value pairs Hey experts how would I turn this string into a usable array? fname=first_name,lname=last_name,phone=phone_number,street1=address,city=city,state=state,zip=zip,carrier=carrier $arr = array();

How to transform the matrix into a digital matrix?

I have made a scoring matrix in text file for local alignment with pairwiseAlignment function. Then I used this function to input it into the R: ex <- as.matrix(read.table("~/scoringMatrix", header=FALSE, sep = "\t", row.names = 1,

How to transform a labyrinth into a graph?

I am trying to convert a maze data structure into a graph. The maze is like a grid and some walls between cells. maze[8][8][4] is how the maze is represented. If maze[i][j][0] = 1 it means you can't go up from (i,j) if maze[i][j][1] = 1 it means you

How to transform the matrix into a matrix with an identity matrix

I want to get transformed array contains identity matrix from n*m array using numpy/scipy. from n*m matrix array([[ a, b, c, d, e, f], [ g, h, i, j, k, l], [ m, n, o, p, q, r]]) to array([[ 1, 0, 0, a', b', c'], [ 0, 1, 0, d', e', f'], [ 0, 0, 1, g',

How to transform a number into its binary representation?

How would I turn a number into denary from binary and whats wrong with the code: print (" Enter a number") realnum = int(input()) print("In Binary that is", bin(realnum)[2:]) input(" Press enter to finish ") print("In Bi

How to transform a tuple into a whole number in Python (examples inside)?

I have a list of tuples: "(1,2,3), (2,3,1)..." I would like to change this into a list of integers: "123, 231..." How might I go about doing this? Thanks in advance.A more functional approach: [reduce(lambda a, x: a * 10 + x, t) for t

Recommend

  • 7

    .Net Core with 微服务 - Seq 日志聚合 上一次我们介绍并演示了如...

  • 2
    • segmentfault.com 3 years ago
    • Cache

    Linux 之 seq 命令

    以指定增量从首数开始打印数字到尾数,seq命令用于产生从某个数到另外一个数之间的所有整数。> seq [选项]... 尾数 > seq [选项]... 首数 尾数 > seq [选项]... 首数 增量 尾数-f, --for...

  • 5
    • www.myfreax.com 3 years ago
    • Cache

    Linux中的seq命令

    seq命令是sequence的缩写,用于以递增或者递减的方式打印数字序列。换句话说,就是打印指定数值的范围。数字可以是整数或带小数点的实数,也可以是负数。你也可以指定序列的上限或者下限等。在本教程中,我们将说明如何使用seq命令和seq常用的命令选项,...

  • 2

    André Slupik Posted on Mar 3...

  • 7

    最近在分析影響效能的 Query,發現 PostgreSQL 有時的查詢效能不如我們預期,用了 EXPLAIN 下去分析索引,發現確實新增的 index 並沒有在 query plain 裡面,我想瞭解為什麼。評估結果是 seq scan 更快在數據量很小的時候, seq scan 會比 inde...

  • 4

    kernel 劫持seq_operations && 利用pt_regs 劫持seq_operations进行栈迁移 seq_operations是一个大小为0x20的结构体,在...

  • 4

    Comparison of SEQ With and Without Numbers Jim Lewis, PhD and Jeff Sauro, PhD...

  • 3
    • measuringu.com 2 years ago
    • Cache

    Evaluation of Three SEQ Variants

    Evaluation of Three SEQ Variants Jeff Sauro, PhD • Jim Lewis, PhD ...

  • 5
    • discretetom.github.io 1 year ago
    • Cache

    理解TCP协议中SEQ与ACK的计算逻辑

    理解TCP协议中SEQ与ACK的计算逻辑 理解TCP协议中SEQ与ACK的计算逻辑 tcpdump 抓包示例(删除了不必要的字段): 172.31.27.207.33944 > 192.168.242.180.27017: Flags [S], seq 1310410798 ... length 0...

  • 7

    Describing SEQ® Scores with Adjectives Jim Lewis, PhD and Jeff Sauro, P...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK