TCL/ITCL - implementing unique data structure using existing data structure - data-structures

I am new to tcl/Itcl programming.
Is there any data structure in tcl or Itcl or any packages provided for those two that can help me implement the next structure (see the picture below).
Explanation of the data structure:
This data structure is very similar to B-Tree data structure.
But , Every level of the data structure is different class meaning class A can have only child of type class B.
Every "class type" in any level (Node) has another data that is unique to this class in addition to the "pointers" to parent and children of this "Node".
I saw there is the struct::tree data structure but I don't really know if I can use this data structure to implement my data structure with those exceptions.
Is there any way to implement it except creating a "node base class" and 3 more class that inherits from this "node base class" with unique list in each one?

It can be implemented as a nested dictionary.
The key names can of course be whatever you want.
For this example, the C nodes simply have a list of data items.
This could be another dictionary instead.
set nodes {
nodea1 {
nodeb1 {
nodec1 { data1 data2 data3 }
nodec2 { data1 data2 data3 }
}
nodeb2 {
}
nodeb3 {
nodec1 { data1 data2 data3 }
nodec2 { data1 data2 data3 }
nodec3 { data1 data2 data3 }
}
}
}
puts [dict get $nodes nodea1 nodeb3 nodec2]

Related

Go gota Dataframe not loading struct, requires []struct

I am reading a file with each row containing two words separated by a space. I scan and split each line to two words (strings) and store them in the MyEntity struct (in items). The words are then added to items.
type Entity struct {
Name string
Entity string
}
type MyEntity struct {
Entities []Entity
}
func (entity *MyEntity) AddEntity(item Entity) []Entity {
entity.Entities = append(entity.Entities, item)
return entity.Entities
}
...
items := MyEntity{}
// loop here over the rows - name is first word, entity is second word
item := Entity{
name, entity,
}
items.AddEntity(item)
...
Should items not be a []struct here? I am using the gota package (https://github.com/go-gota/gota) to create a DataFrame from items but using dataframe.LoadStructs(items) fails because items is a struct and it must be a []struct.
I am fairly new to Go, so what am I missing here?
Load the data frame from the slice of entities:
df := dataframe.LoadStructs(items.Entities)

terrraform get yaml values

I need to get the values from the yaml snippet into 1. the Route53 Zones where I need the apex_nme to be the zone names and 2. the records to be added as Route53 records into the specific zones. I have no clue how to do this. Any help is highly appreciated.
resource "aws_route53_zone" "this" {
for_each = {
for apex in var.source_domains : apex => {
name = apex.name
}
}
}
resource "aws_route53_record" "this" {
for_each = {
for records in var.source_domains : records => {
zone_id = aws_route53_zone.this[each.key].zone_id
name = subdomains.records
type = "A"
records = ["192.168.0.1"]
}
}
}
source_domains:
- apax_name: elastic2ls.com
records:
- elastic2ls.com
- www.elastic2ls.com
- apax_name: elastic2ls.ch
records:
- elastic2ls.ch
- www.elastic2ls.ch
- image.elastic2ls.ch
- m.elastic2ls.ch
- static.elastic2ls.ch
Your question is presented as if it's about YAML parsing, but I suspect you're really asking about how to write the for_each expression for aws_route53_record.this to create all of the records across all of the domains.
For completeness, I'll note that you could get var.source_domains to be populated from that YAML by making the calling module define that variable with an expression like this:
module "example" {
source = "../modules/example"
source_domains = yamldecode(file("${path.module}/example.yaml")).source_domains
# ...
}
Inside the module itself then, I'd first declare that module with a suitable type constraint to make it clear what data structure we're expecting, like this:
variable "source_domains" {
type = set(object({
apax_name = string
records = set(string)
}))
}
Here I defined both the top-level structure and the nested record as set types, because the way we're going to use them means that the ordering isn't important and we're expecting them all to be unique.
With all of that in place we can start to write out the resource definitions. Let's start with the zones, which are the simpler case because var.source_domains already meets the main requirement of having one element per resource instance we want to declare:
resource "aws_route53_zone" "example" {
for_each = {
for d in var.source_domains : d.apax_name => d
}
name = each.value.apax_name
}
With the example YAML input you shared, this block will declare two instances of this resource:
aws_route53_zone.example["elastic2ls.com"]
aws_route53_zone.example["elastic2ls.ch"]
The aws_route53_record declaration is a little trickier because we need to project the input data structure into a new structure where there's one element per record we want to declare, rather than one element per zone. Flattening nested data structures for for_each is a common use for the flatten function, and we can adapt the networks and subnets example from the documentation to work with zones and records instead:
locals {
zone_records = flatten([
for d in var.source_domains : [
for r in d.records : {
zone_name = d.apax_name
zone_id = aws_route53_zone.example[d.apax_name].id
record = r
}
]
])
}
This local value is constructing a list of objects where each object represents one valid pairing of zone and record. That means that the number of elements matches the number of aws_route53_record instances we need to declare, and so we can use this data structure in for_each:
resource "aws_route53_record" "example" {
for_each = {
for zr in local.zone_records : zr.record => zr
}
zone_id = each.value.zone_id
name = each.value.record
# ...
}
This example diverges a little from the typical flatten/for_each pattern because all of your record names already have the zone name embedded in them anyway, and so we don't need the usual expression to generate a compound unique key with multiple parts, like "${subnet.network_key}.${subnet.subnet_key}" in the documentation's example. The record name alone is sufficient for a unique key across all pairs in this case.
This then, again based on your example YAML, will declare the following instances:
aws_route53_record.example["elastic2ls.com"]
aws_route53_record.example["www.elastic2ls.com"]
aws_route53_record.example["elastic2ls.ch"]
aws_route53_record.example["www.elastic2ls.ch"]
aws_route53_record.example["image.elastic2ls.ch"]
aws_route53_record.example["m.elastic2ls.ch"]
aws_route53_record.example["static.elastic2ls.ch"]

I need a type for a graphql property that could be two 2 types

So I have some code like the following:
input Data {
activityValue: Int
}
But I need it to be something more like
input Data {
activityValue: Int | String!
}
I know in typescript, even though frowned upon you can use any or number | string. Is there anything like this in graphql?
There is no real such thing as multiple types in the GraphQL specification. However, Unions can fit your needs.
From the specification:
GraphQL Unions represent an object that could be one of a list of GraphQL Object types, but provides for no guaranteed fields between those types.
That means that Unions can include types but no scalars or lists.
For example, a union can be declared like this:
union Media = Book | Movie
And then be used as a type:
type Query {
allMedia: [Media] # This list can include both Book and Movie objects
}
Example is taken from Apollo Docs.
If you want to check in your query if you have some type of the Union type, then you need to do that with inline fragments.
query Test {
singleMedia(id: 123) {
name
... on Book {
author
}
... on Movie {
musicTitle
}
}
}

How to parse JSON in haskell

I need to parse a JSON incoming form the UI into a data structure.
the data structure is a combination of other data structures
Data Collection = Collection { t1 :: t1 , t2::t2}
newtype t1 = t1 {unt1 :: String}
data t2 = t2 {id :: Integer, rank :: String}
The data I am getting is in the format
{
"t1": {
"_unt1": "at1Value"
},
"t2": {
"id" : 1
"rank": "Officer"
}
}
I basically need to create a Collection Data type . How should I do this in the simplest way ?
I tried the Aeson library and made Collection and instance of JSON ,and then tried something like
decode data :: Maybe Collection
But that doesn't work. I did try looking into the parsec library as well, But I am not sure whether that will be useful here.
I am pretty new to haskell, so maybe I am missing something here. What would be the best way to implement this taking into consideration the actual data structure might be much more complicated than the example I gave, with it going several layers deep
import Text.JSON.Generic and decode the json.
import Text.JSON.Generic
................
decodeJSON data :: Collection

Is there a way, using LINQ/EF, to get the top most item in a parent/child hierarchy?

I have a class called Structure:
public class Structure
{
public int StructureId { get; set; }
public Structure Parent { get; set; }
}
As you can see, Structure has a parent Structure. There can be an indefinite number of structures within this hierarchy.
Is there any way, using LINQ (with Entity Framework), to get the top-most structure in this hierarchy?
Currently, I'm having to hit the database quite a few times in order to find the top most parent. The top most parent is a Structure with a null Parent property:
Structure structure = structureRepository.Get(id);
while (structure.Parent != null)
{
structure = structureRepository.Get(structure.Parent.StructureId);
}
// When we're here; `structure` is now the top most parent.
So, is there any elegant way to do this using LINQ/Lambdas? Ideally, starting with the following code:
var structureQuery = from item in context.Structures
where item.StructureId == structureId
select item;
I just want to be able to write something like the following so that I only fire off one database hit:
structureQuery = Magic(structureQuery);
Structure topMostParent = structureQuery.Single();
This is not a direct answer, but the problem you are having is related to the way you are storing your tree. There are a couple ways of simplifying this query by structuring data differently.
One is to use a Nested Set Hierarchy, which can simplify many kinds of queries across trees.
Another is to store a denomralized table of Ancestor/Descendant/Depth tuples. This query then becomes finding the tuple with the current structure as the descendant with the maximum depth.
I think the best I'm going to get is to load the entire hierarchy in one hit from the structure I want the top parent of:
var structureQuery = from item in context.Structures
.Include(x => x.Parent)
where item.StructureId == structureId
select item;
Then just use the code:
while (structure.Parent != null)
{
structure = structure.Parent;
}
I have a similar situation. I didn't manage to solve it directly with LINQ/EF. Instead I solved by creating a database view using recursive common table expressions, as outlined here. I made a user-defined function that cross applies all parents to a child (or vice versa), then a view that makes use of this user-defined function which I imported into my EF object context.
(disclaimer: simplified code, I didn't actually test this)
I have two tables, say MyTable (containing all items) and MyParentChildTable containing the ChildId,ParentId relation
I have then defined the following udf:
CREATE FUNCTION dbo.fn_getsupertree(#childid AS INT)
RETURNS #TREE TABLE
(
ChildId INT NOT NULL
,ParentId INT NULL
,Level INT NOT NULL
)
AS
BEGIN
WITH Parent_Tree(ChildId, ParentId)
AS
(
-- Anchor Member (AM)
SELECT ChildId, ParentId, 0
FROM MyParentChildTable
WHERE ChildId = #childid
UNION all
-- Recursive Member (RM)
SELECT info.ChildId, info.ParentId, tree.[Level]+1
FROM MyParentChildTable AS info
JOIN Parent_Tree AS tree
ON info.ChildId = tree.ParentId
)
INSERT INTO #TREE
SELECT * FROM Parent_Tree;
RETURN
END
and the following view:
CREATE VIEW VwSuperTree AS (
SELECT tree.*
FROM MyTable
CROSS APPLY fn_getsupertree(MyTable.Id) as tree
)
GO
This gives me for each child, all parents with their 'tree level' (direct parent has level 1, parent of parent has level 2, etc.). From that view, it's easy to query the item with the highest level. I just imported the view in my EF context to be able to query it with LINQ.
I like the question and can't think of a linq-y way of doing this. But could you perhaps implement this on your repository class? After all, there should be only one at the top and if the need for it is there, then maybe it deserves a structureRepository.GetRoot() or something.
you can use the linq take construct, for instance
var first3Customers = (
from c in customers
select new {c.CustomerID, c.CustomerName} )
.Take(2);

Resources