Combining modules fails - elm

I want to connect all the models but I always get those cycle-errors.
My whole model looks like this:
type alias Model =
{ users : List User
, blogs : List Blog
, posts : List Post
, comments : List Comment
, labels : List Label
}
Sadly there appears the error message:
Your module imports form a cycle:
┌─────┐
│ Features.User
│ ↓
│ Features.Blog
│ ↓
│ Features.Label
│ ↓
│ Features.Post
│ ↓
│ Features.Comment
└─────┘
This is the user file:
module Features.User exposing (..)
import Features.Blog as Blog
import Features.Comment as Comment
import Features.Post as Post
type alias Id =
String
type alias User =
{ id : String
, name : String
, email : String
, password : String
, blogs : List Blog.Id
, posts : List Post.Id
, comments : List Comment.Id
}
I would love to get some input how to restructure the code so that it works just fine. Thanks.

Related

How can we dynamically generate a list of map in terraform?

I have a list of rules which i want to generate at runtime as it depends on availability_domains where availability_domains is a list
availability_domains = [XX,YY,ZZ]
locals {
rules = [{
ad = XX
name = "service-XX",
hostclass = "hostClassName",
instance_shape = "VM.Standard2.1"
...
},{
ad = YY
name = "service-YY",
hostclass = "hostClassName",
instance_shape = "VM.Standard2.1"
...
}, ...]
}
Here, all the values apart from ad and name are constant. And I need rule for each availability_domains.
I read about null_resource where triggers can be used to generate this but i don't want to use a hack here.
Is there any other way to generate this list of map?
Thanks for help.
First, you need to fix the availability_domains list to be a list of strings.
availability_domains = ["XX","YY","ZZ"]
Assuming availability_domains is a local you just run a forloop on it.
locals {
availability_domains = ["XX","YY","ZZ"]
all_rules = {"rules" = [for val in local.availability_domains : { "ad" : val, "name" : "service-${val}" , "hostclass" : "hostClassName", "instance_shape" : "VM.Standard2.1"}] }
}
or if you dont want the top level name to the array then this should work as well
locals {
availability_domains = ["XX","YY","ZZ"]
rules = [for val in local.availability_domains : { "ad" : val, "name" : "service-${val}" , "hostclass" : "hostClassName", "instance_shape" : "VM.Standard2.1"}]
}

How to get a particular key value pair from elm object

I have a elm object which returns value are as follows
type alias Info =
{ name : String
, stdId : stdKey
, pemId : PermanentKey
}
This info lies in a page named as class.elm
in another page i want to use the std key alone to do a if else comparison.
i tied to assign the stdKey to a variable like as follows
uniKey = class.Info.stdId
But the elm doesn't accept this way .
Kindly help.
Assuming you intend to create a Info value in the Class.elm file and use it in an Other.elm file :
file Class.elm
module Class exposing (Info)
type alias Info =
{ name : String
, stdId : StdKey
, pemId : PermanentKey
}
type alias StdKey = String
type alias PermanentKey = String
info : Info
info =
{ name = "name"
, stdKey = "valueForStdKey"
, pemKey = "valueForPemKey"
}
file Other.elm
module Other exposing (..)
import Class
uniKey : Class.StdKey
uniKey = Class.info.stdKey
Alternative
file Alt.elm
module Alt exposing (..)
import Class exposing (StdKey, PermanentKey, info)
uniKey : StdKey
uniKey = info.stdKey
Elm automatically generates a .stdId function that works on any record with that field
.stdId info == info.stdId

Elm union subsets

Say I have a union type like this:
type Route
= Home
| License
| UserProfile { username : String }
| Search { query : String }
| SomeOtherPage
In practice I frequently need to work with subsets of this union. For example:
type StaticRoute = Home | License
I would like to be able to define functions which accept subsets like the above, instead of the wider Route.
I don't want to nest StaticRoute inside of Route, like so:
type Route
= Static StaticRoute
| UserProfile { username : String }
| Search { query : String }
| SomeOtherPage
This is because I want to be able to define many different subsets of Route, some of which could overlap:
type StaticRoute = Home | License
type RouteWithServerRendering = Home | Search { query : String }
type LoggedInRoute = SomeOtherPage
-- and so on…
How then can I define subsets of Route without repeating definitions?
Jasper Woudenberg recently posted Conversion functions, five stars
, which advocates for having similar types and using conversion functions to translate between one type to another.
In your case, it might look like this:
module Route exposing (fromStaticRoute, toStaticRoute)
fromStaticRoute : StaticRoute -> Route
fromStaticRoute staticRoute =
case staticRoute of
Static.Home ->
Home
Static.License ->
License
toStaticRoute : Route -> Maybe StaticRoute
toStaticRoute route =
case route of
Home ->
Just Static.Home
License ->
Just Static.License
_ ->
Nothing

Is it possible to use Maybe with Extensible Records?

I am trying to write a function to pull the maybe off a list of extensible records, I am wondering if this is possible. Source code is below, or see Ellie link here
module Temp exposing (..)
import Html exposing (text)
main =
text "Hello"
items : Maybe List { data | id : Int } -> List { data | id : Int }
items maybeList =
case maybeList of
Just t ->
t
Nothing ->
[]
Maybe List { data | id : Int } parses as Maybe (List) ({ data | id : Int }). I'm not sure why the error message is so misleading, but the fix is to wrap List ... in () like this:
items : Maybe (List { data | id : Int }) -> List { data | id : Int }
^ ^
Edit: also, your function can be simplified using Maybe.withDefault:
items = Maybe.withDefault []

How can I refactor two functions into one function that takes a generic argument?

How can I refactor two functions into one function that has a generic parameter?
Example:
getVideo : Video -> Post
getVideo video =
let
(Video post) =
video
in
post
getPodcast : Podcast -> Post
getPodcast podcast =
let
(Podcast post) =
podcast
in
post
I would like to do something like this:
getPodcast : 'a -> Post
getPodcast 'a =
let
('a post) =
'a
in
post
Appendix:
type Video
= Video Post
type Podcast
= Podcast Post
You cannot have such an open-ended generic function in Elm. Here are two options:
Use a container type
You can create a container type that has a constructor for each of its valid types:
type PostContainer
= VideoContainer Video
| PodcastContainer Podcast
Now your getPost function consists of a case statement which returns the appropriate post.
getPost : PostContainer -> Post
getPost container =
case container of
VideoContainer (Video post) ->
post
PodcastContainer (Podcast post) ->
post
Include the post type in the Post value
Let's say your Post object looks like this:
type alias Post =
{ name : String
, body : String
}
You could create an enumeration of post types like this:
type PostType = Video | Podcast
You could redefine Post to include the type:
type alias Post =
{ name : String
, body : String
, postType : PostType
}
Or, if you choose to keep the post body separate from the type, you could do something like this:
type alias PostContents =
{ name : String
, body : String
}
type Post = Post PostType PostContents
and your getPostContents function would simply be
getPostContents : Post -> PostContents
getPostContents _ contents =
contents