Function that retrieves element from HList (while preserving its type)
I have this type which will be generated via shapeless:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Basically I have a bunch of case objects extending a trait so I managed to create a method which gives me instances of all case objects as an HList
Then using import shapeless.ops.hlist.Last
and init
I wrote a method to retrieve one of the nodes in the HList if the value is equal to the string "student":
def getLast(hl:hlistt) = {
val last0=Last[hlistt]
val la=last0(hl)
if (la.value == "student") la
else init(hl)
}
The issue is that if I call this method I will not get the correct node type from the HList.
getLast(STUDENT :: AUTO_LOANS :: HNil)
The method works and returns the node but the type is off:
Product with Serializable = STUDENT :: HNil
Do I need some Witness/Aux implicits to return the correct type?
scala shapeless hlist
add a comment |
I have this type which will be generated via shapeless:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Basically I have a bunch of case objects extending a trait so I managed to create a method which gives me instances of all case objects as an HList
Then using import shapeless.ops.hlist.Last
and init
I wrote a method to retrieve one of the nodes in the HList if the value is equal to the string "student":
def getLast(hl:hlistt) = {
val last0=Last[hlistt]
val la=last0(hl)
if (la.value == "student") la
else init(hl)
}
The issue is that if I call this method I will not get the correct node type from the HList.
getLast(STUDENT :: AUTO_LOANS :: HNil)
The method works and returns the node but the type is off:
Product with Serializable = STUDENT :: HNil
Do I need some Witness/Aux implicits to return the correct type?
scala shapeless hlist
1
That's not the correct code to reproduce the result you describe. For one thing,getLast()
, as posted, is not recursive.
– jwvh
Nov 20 '18 at 9:00
@jwvh good point - I wrote this late at night (my time) and initially was thinking of a recursive fn. I did mean to wrapinit(hl)
intogetLast(init(hl))
– Adrian
Nov 20 '18 at 17:47
add a comment |
I have this type which will be generated via shapeless:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Basically I have a bunch of case objects extending a trait so I managed to create a method which gives me instances of all case objects as an HList
Then using import shapeless.ops.hlist.Last
and init
I wrote a method to retrieve one of the nodes in the HList if the value is equal to the string "student":
def getLast(hl:hlistt) = {
val last0=Last[hlistt]
val la=last0(hl)
if (la.value == "student") la
else init(hl)
}
The issue is that if I call this method I will not get the correct node type from the HList.
getLast(STUDENT :: AUTO_LOANS :: HNil)
The method works and returns the node but the type is off:
Product with Serializable = STUDENT :: HNil
Do I need some Witness/Aux implicits to return the correct type?
scala shapeless hlist
I have this type which will be generated via shapeless:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Basically I have a bunch of case objects extending a trait so I managed to create a method which gives me instances of all case objects as an HList
Then using import shapeless.ops.hlist.Last
and init
I wrote a method to retrieve one of the nodes in the HList if the value is equal to the string "student":
def getLast(hl:hlistt) = {
val last0=Last[hlistt]
val la=last0(hl)
if (la.value == "student") la
else init(hl)
}
The issue is that if I call this method I will not get the correct node type from the HList.
getLast(STUDENT :: AUTO_LOANS :: HNil)
The method works and returns the node but the type is off:
Product with Serializable = STUDENT :: HNil
Do I need some Witness/Aux implicits to return the correct type?
scala shapeless hlist
scala shapeless hlist
edited Nov 20 '18 at 17:46
Adrian
asked Nov 20 '18 at 7:34
AdrianAdrian
3,88573970
3,88573970
1
That's not the correct code to reproduce the result you describe. For one thing,getLast()
, as posted, is not recursive.
– jwvh
Nov 20 '18 at 9:00
@jwvh good point - I wrote this late at night (my time) and initially was thinking of a recursive fn. I did mean to wrapinit(hl)
intogetLast(init(hl))
– Adrian
Nov 20 '18 at 17:47
add a comment |
1
That's not the correct code to reproduce the result you describe. For one thing,getLast()
, as posted, is not recursive.
– jwvh
Nov 20 '18 at 9:00
@jwvh good point - I wrote this late at night (my time) and initially was thinking of a recursive fn. I did mean to wrapinit(hl)
intogetLast(init(hl))
– Adrian
Nov 20 '18 at 17:47
1
1
That's not the correct code to reproduce the result you describe. For one thing,
getLast()
, as posted, is not recursive.– jwvh
Nov 20 '18 at 9:00
That's not the correct code to reproduce the result you describe. For one thing,
getLast()
, as posted, is not recursive.– jwvh
Nov 20 '18 at 9:00
@jwvh good point - I wrote this late at night (my time) and initially was thinking of a recursive fn. I did mean to wrap
init(hl)
into getLast(init(hl))
– Adrian
Nov 20 '18 at 17:47
@jwvh good point - I wrote this late at night (my time) and initially was thinking of a recursive fn. I did mean to wrap
init(hl)
into getLast(init(hl))
– Adrian
Nov 20 '18 at 17:47
add a comment |
2 Answers
2
active
oldest
votes
la
is of type AUTO_LOANS.type
, init(hl)
is of type STUDENT.type :: HNil
, so
if (la.value == "student") la
else init(hl)
is of type Any
(or Product with Serializable
).
If you would like to return values of different types from different branches you need a Poly
.
import shapeless.{Poly1, Witness}
object myPoly extends Poly1 {
implicit def studentCase: Case.Aux[Witness.`"student"`.T, STUDENT.type] =
at(_ => STUDENT)
implicit def autoLoansCase: Case.Aux[Witness.`"auto-loans"`.T, AUTO_LOANS.type] =
at(_ => AUTO_LOANS)
}
import shapeless.syntax.singleton._
println(
myPoly("student".narrow)
) // STUDENT
println(
myPoly("auto-loans".narrow)
) // AUTO_LOANS
// println(
// myPoly("abc".narrow)
// ) // doesn't compile
This approach works if the string is known at compile time.
add a comment |
I am not quite sure what you want to do. Given:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Last[hlistt]
will resolve to AUTO_LOANS.type
(your true if branch)
while init
will resolve to STUDENT :: HNil
(your false if branch)
the LUB (least upper bound) of these types will be Product with Serializable
so that's why you see that.
If you want to check a runtime property of a member of the hlist you'll have to thread the appropriate typebounds and result types through by deriving them with the appropriate machinery. In this case that is already given by shapeless.
https://scalafiddle.io/sf/fdtn3cz/0
Is this what you wanted?
EDIT:
I also just read
I have this type which will be generated dynamically:
what do you exactly mean by "dynamically"? because unless you can specify some compile time properties, shapeless might not be the solution you're looking for.
You got very close to what I want. Do you know why I can't do:if(last(hlist).value == "student") Some(last(hlist)) else getLastOrNone(init(hlist))
and adding a case forHNil
. I get a type mismatch. I'm trying to traverse the HList and do a find node by value instead of by type.
– Adrian
Nov 20 '18 at 17:40
the problem with that is that you won't know at compile time what the output type of your function is because it depends on runtime values. What is your motivation to useHList
?
– Dominic Egger
Nov 20 '18 at 17:53
I want to preserve the types ofCar
andStudent
in your sample code. The actual problem I'm having is given a string "student" or "car" get the correct singleton ie Student or Car. I have type classes defined on these singleton types which is why I'm trying really hard to preserve types. I'm also looking into making some mapping of "car"->Car,"student"->Student so when I'm given a text I can use it as a key to get the appropriate type
– Adrian
Nov 20 '18 at 18:29
1
well technically I guess you could formulate a function that goes from some hlistA :: B :: C :: HNil
toA :+: B :+: C :+: Unit :+: CNil
and finds the first match for some predicate poly-function or returnsUnit
. But because you base this off runtime values I don't think you can do it directly. what would the returntype of that function be? so it'd go more towards a solution as Dmytro said bellow.
– Dominic Egger
Nov 20 '18 at 18:47
Ya that's my roadblock: don't know what the type of that method would be :(
– Adrian
Nov 20 '18 at 18:55
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388226%2ffunction-that-retrieves-element-from-hlist-while-preserving-its-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
la
is of type AUTO_LOANS.type
, init(hl)
is of type STUDENT.type :: HNil
, so
if (la.value == "student") la
else init(hl)
is of type Any
(or Product with Serializable
).
If you would like to return values of different types from different branches you need a Poly
.
import shapeless.{Poly1, Witness}
object myPoly extends Poly1 {
implicit def studentCase: Case.Aux[Witness.`"student"`.T, STUDENT.type] =
at(_ => STUDENT)
implicit def autoLoansCase: Case.Aux[Witness.`"auto-loans"`.T, AUTO_LOANS.type] =
at(_ => AUTO_LOANS)
}
import shapeless.syntax.singleton._
println(
myPoly("student".narrow)
) // STUDENT
println(
myPoly("auto-loans".narrow)
) // AUTO_LOANS
// println(
// myPoly("abc".narrow)
// ) // doesn't compile
This approach works if the string is known at compile time.
add a comment |
la
is of type AUTO_LOANS.type
, init(hl)
is of type STUDENT.type :: HNil
, so
if (la.value == "student") la
else init(hl)
is of type Any
(or Product with Serializable
).
If you would like to return values of different types from different branches you need a Poly
.
import shapeless.{Poly1, Witness}
object myPoly extends Poly1 {
implicit def studentCase: Case.Aux[Witness.`"student"`.T, STUDENT.type] =
at(_ => STUDENT)
implicit def autoLoansCase: Case.Aux[Witness.`"auto-loans"`.T, AUTO_LOANS.type] =
at(_ => AUTO_LOANS)
}
import shapeless.syntax.singleton._
println(
myPoly("student".narrow)
) // STUDENT
println(
myPoly("auto-loans".narrow)
) // AUTO_LOANS
// println(
// myPoly("abc".narrow)
// ) // doesn't compile
This approach works if the string is known at compile time.
add a comment |
la
is of type AUTO_LOANS.type
, init(hl)
is of type STUDENT.type :: HNil
, so
if (la.value == "student") la
else init(hl)
is of type Any
(or Product with Serializable
).
If you would like to return values of different types from different branches you need a Poly
.
import shapeless.{Poly1, Witness}
object myPoly extends Poly1 {
implicit def studentCase: Case.Aux[Witness.`"student"`.T, STUDENT.type] =
at(_ => STUDENT)
implicit def autoLoansCase: Case.Aux[Witness.`"auto-loans"`.T, AUTO_LOANS.type] =
at(_ => AUTO_LOANS)
}
import shapeless.syntax.singleton._
println(
myPoly("student".narrow)
) // STUDENT
println(
myPoly("auto-loans".narrow)
) // AUTO_LOANS
// println(
// myPoly("abc".narrow)
// ) // doesn't compile
This approach works if the string is known at compile time.
la
is of type AUTO_LOANS.type
, init(hl)
is of type STUDENT.type :: HNil
, so
if (la.value == "student") la
else init(hl)
is of type Any
(or Product with Serializable
).
If you would like to return values of different types from different branches you need a Poly
.
import shapeless.{Poly1, Witness}
object myPoly extends Poly1 {
implicit def studentCase: Case.Aux[Witness.`"student"`.T, STUDENT.type] =
at(_ => STUDENT)
implicit def autoLoansCase: Case.Aux[Witness.`"auto-loans"`.T, AUTO_LOANS.type] =
at(_ => AUTO_LOANS)
}
import shapeless.syntax.singleton._
println(
myPoly("student".narrow)
) // STUDENT
println(
myPoly("auto-loans".narrow)
) // AUTO_LOANS
// println(
// myPoly("abc".narrow)
// ) // doesn't compile
This approach works if the string is known at compile time.
edited Nov 20 '18 at 10:14
answered Nov 20 '18 at 10:05
Dmytro MitinDmytro Mitin
6,374515
6,374515
add a comment |
add a comment |
I am not quite sure what you want to do. Given:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Last[hlistt]
will resolve to AUTO_LOANS.type
(your true if branch)
while init
will resolve to STUDENT :: HNil
(your false if branch)
the LUB (least upper bound) of these types will be Product with Serializable
so that's why you see that.
If you want to check a runtime property of a member of the hlist you'll have to thread the appropriate typebounds and result types through by deriving them with the appropriate machinery. In this case that is already given by shapeless.
https://scalafiddle.io/sf/fdtn3cz/0
Is this what you wanted?
EDIT:
I also just read
I have this type which will be generated dynamically:
what do you exactly mean by "dynamically"? because unless you can specify some compile time properties, shapeless might not be the solution you're looking for.
You got very close to what I want. Do you know why I can't do:if(last(hlist).value == "student") Some(last(hlist)) else getLastOrNone(init(hlist))
and adding a case forHNil
. I get a type mismatch. I'm trying to traverse the HList and do a find node by value instead of by type.
– Adrian
Nov 20 '18 at 17:40
the problem with that is that you won't know at compile time what the output type of your function is because it depends on runtime values. What is your motivation to useHList
?
– Dominic Egger
Nov 20 '18 at 17:53
I want to preserve the types ofCar
andStudent
in your sample code. The actual problem I'm having is given a string "student" or "car" get the correct singleton ie Student or Car. I have type classes defined on these singleton types which is why I'm trying really hard to preserve types. I'm also looking into making some mapping of "car"->Car,"student"->Student so when I'm given a text I can use it as a key to get the appropriate type
– Adrian
Nov 20 '18 at 18:29
1
well technically I guess you could formulate a function that goes from some hlistA :: B :: C :: HNil
toA :+: B :+: C :+: Unit :+: CNil
and finds the first match for some predicate poly-function or returnsUnit
. But because you base this off runtime values I don't think you can do it directly. what would the returntype of that function be? so it'd go more towards a solution as Dmytro said bellow.
– Dominic Egger
Nov 20 '18 at 18:47
Ya that's my roadblock: don't know what the type of that method would be :(
– Adrian
Nov 20 '18 at 18:55
|
show 1 more comment
I am not quite sure what you want to do. Given:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Last[hlistt]
will resolve to AUTO_LOANS.type
(your true if branch)
while init
will resolve to STUDENT :: HNil
(your false if branch)
the LUB (least upper bound) of these types will be Product with Serializable
so that's why you see that.
If you want to check a runtime property of a member of the hlist you'll have to thread the appropriate typebounds and result types through by deriving them with the appropriate machinery. In this case that is already given by shapeless.
https://scalafiddle.io/sf/fdtn3cz/0
Is this what you wanted?
EDIT:
I also just read
I have this type which will be generated dynamically:
what do you exactly mean by "dynamically"? because unless you can specify some compile time properties, shapeless might not be the solution you're looking for.
You got very close to what I want. Do you know why I can't do:if(last(hlist).value == "student") Some(last(hlist)) else getLastOrNone(init(hlist))
and adding a case forHNil
. I get a type mismatch. I'm trying to traverse the HList and do a find node by value instead of by type.
– Adrian
Nov 20 '18 at 17:40
the problem with that is that you won't know at compile time what the output type of your function is because it depends on runtime values. What is your motivation to useHList
?
– Dominic Egger
Nov 20 '18 at 17:53
I want to preserve the types ofCar
andStudent
in your sample code. The actual problem I'm having is given a string "student" or "car" get the correct singleton ie Student or Car. I have type classes defined on these singleton types which is why I'm trying really hard to preserve types. I'm also looking into making some mapping of "car"->Car,"student"->Student so when I'm given a text I can use it as a key to get the appropriate type
– Adrian
Nov 20 '18 at 18:29
1
well technically I guess you could formulate a function that goes from some hlistA :: B :: C :: HNil
toA :+: B :+: C :+: Unit :+: CNil
and finds the first match for some predicate poly-function or returnsUnit
. But because you base this off runtime values I don't think you can do it directly. what would the returntype of that function be? so it'd go more towards a solution as Dmytro said bellow.
– Dominic Egger
Nov 20 '18 at 18:47
Ya that's my roadblock: don't know what the type of that method would be :(
– Adrian
Nov 20 '18 at 18:55
|
show 1 more comment
I am not quite sure what you want to do. Given:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Last[hlistt]
will resolve to AUTO_LOANS.type
(your true if branch)
while init
will resolve to STUDENT :: HNil
(your false if branch)
the LUB (least upper bound) of these types will be Product with Serializable
so that's why you see that.
If you want to check a runtime property of a member of the hlist you'll have to thread the appropriate typebounds and result types through by deriving them with the appropriate machinery. In this case that is already given by shapeless.
https://scalafiddle.io/sf/fdtn3cz/0
Is this what you wanted?
EDIT:
I also just read
I have this type which will be generated dynamically:
what do you exactly mean by "dynamically"? because unless you can specify some compile time properties, shapeless might not be the solution you're looking for.
I am not quite sure what you want to do. Given:
type hlistt = STUDENT.type :: AUTO_LOANS.type :: HNil
Last[hlistt]
will resolve to AUTO_LOANS.type
(your true if branch)
while init
will resolve to STUDENT :: HNil
(your false if branch)
the LUB (least upper bound) of these types will be Product with Serializable
so that's why you see that.
If you want to check a runtime property of a member of the hlist you'll have to thread the appropriate typebounds and result types through by deriving them with the appropriate machinery. In this case that is already given by shapeless.
https://scalafiddle.io/sf/fdtn3cz/0
Is this what you wanted?
EDIT:
I also just read
I have this type which will be generated dynamically:
what do you exactly mean by "dynamically"? because unless you can specify some compile time properties, shapeless might not be the solution you're looking for.
answered Nov 20 '18 at 9:29


Dominic EggerDominic Egger
77317
77317
You got very close to what I want. Do you know why I can't do:if(last(hlist).value == "student") Some(last(hlist)) else getLastOrNone(init(hlist))
and adding a case forHNil
. I get a type mismatch. I'm trying to traverse the HList and do a find node by value instead of by type.
– Adrian
Nov 20 '18 at 17:40
the problem with that is that you won't know at compile time what the output type of your function is because it depends on runtime values. What is your motivation to useHList
?
– Dominic Egger
Nov 20 '18 at 17:53
I want to preserve the types ofCar
andStudent
in your sample code. The actual problem I'm having is given a string "student" or "car" get the correct singleton ie Student or Car. I have type classes defined on these singleton types which is why I'm trying really hard to preserve types. I'm also looking into making some mapping of "car"->Car,"student"->Student so when I'm given a text I can use it as a key to get the appropriate type
– Adrian
Nov 20 '18 at 18:29
1
well technically I guess you could formulate a function that goes from some hlistA :: B :: C :: HNil
toA :+: B :+: C :+: Unit :+: CNil
and finds the first match for some predicate poly-function or returnsUnit
. But because you base this off runtime values I don't think you can do it directly. what would the returntype of that function be? so it'd go more towards a solution as Dmytro said bellow.
– Dominic Egger
Nov 20 '18 at 18:47
Ya that's my roadblock: don't know what the type of that method would be :(
– Adrian
Nov 20 '18 at 18:55
|
show 1 more comment
You got very close to what I want. Do you know why I can't do:if(last(hlist).value == "student") Some(last(hlist)) else getLastOrNone(init(hlist))
and adding a case forHNil
. I get a type mismatch. I'm trying to traverse the HList and do a find node by value instead of by type.
– Adrian
Nov 20 '18 at 17:40
the problem with that is that you won't know at compile time what the output type of your function is because it depends on runtime values. What is your motivation to useHList
?
– Dominic Egger
Nov 20 '18 at 17:53
I want to preserve the types ofCar
andStudent
in your sample code. The actual problem I'm having is given a string "student" or "car" get the correct singleton ie Student or Car. I have type classes defined on these singleton types which is why I'm trying really hard to preserve types. I'm also looking into making some mapping of "car"->Car,"student"->Student so when I'm given a text I can use it as a key to get the appropriate type
– Adrian
Nov 20 '18 at 18:29
1
well technically I guess you could formulate a function that goes from some hlistA :: B :: C :: HNil
toA :+: B :+: C :+: Unit :+: CNil
and finds the first match for some predicate poly-function or returnsUnit
. But because you base this off runtime values I don't think you can do it directly. what would the returntype of that function be? so it'd go more towards a solution as Dmytro said bellow.
– Dominic Egger
Nov 20 '18 at 18:47
Ya that's my roadblock: don't know what the type of that method would be :(
– Adrian
Nov 20 '18 at 18:55
You got very close to what I want. Do you know why I can't do:
if(last(hlist).value == "student") Some(last(hlist)) else getLastOrNone(init(hlist))
and adding a case for HNil
. I get a type mismatch. I'm trying to traverse the HList and do a find node by value instead of by type.– Adrian
Nov 20 '18 at 17:40
You got very close to what I want. Do you know why I can't do:
if(last(hlist).value == "student") Some(last(hlist)) else getLastOrNone(init(hlist))
and adding a case for HNil
. I get a type mismatch. I'm trying to traverse the HList and do a find node by value instead of by type.– Adrian
Nov 20 '18 at 17:40
the problem with that is that you won't know at compile time what the output type of your function is because it depends on runtime values. What is your motivation to use
HList
?– Dominic Egger
Nov 20 '18 at 17:53
the problem with that is that you won't know at compile time what the output type of your function is because it depends on runtime values. What is your motivation to use
HList
?– Dominic Egger
Nov 20 '18 at 17:53
I want to preserve the types of
Car
and Student
in your sample code. The actual problem I'm having is given a string "student" or "car" get the correct singleton ie Student or Car. I have type classes defined on these singleton types which is why I'm trying really hard to preserve types. I'm also looking into making some mapping of "car"->Car,"student"->Student so when I'm given a text I can use it as a key to get the appropriate type– Adrian
Nov 20 '18 at 18:29
I want to preserve the types of
Car
and Student
in your sample code. The actual problem I'm having is given a string "student" or "car" get the correct singleton ie Student or Car. I have type classes defined on these singleton types which is why I'm trying really hard to preserve types. I'm also looking into making some mapping of "car"->Car,"student"->Student so when I'm given a text I can use it as a key to get the appropriate type– Adrian
Nov 20 '18 at 18:29
1
1
well technically I guess you could formulate a function that goes from some hlist
A :: B :: C :: HNil
to A :+: B :+: C :+: Unit :+: CNil
and finds the first match for some predicate poly-function or returns Unit
. But because you base this off runtime values I don't think you can do it directly. what would the returntype of that function be? so it'd go more towards a solution as Dmytro said bellow.– Dominic Egger
Nov 20 '18 at 18:47
well technically I guess you could formulate a function that goes from some hlist
A :: B :: C :: HNil
to A :+: B :+: C :+: Unit :+: CNil
and finds the first match for some predicate poly-function or returns Unit
. But because you base this off runtime values I don't think you can do it directly. what would the returntype of that function be? so it'd go more towards a solution as Dmytro said bellow.– Dominic Egger
Nov 20 '18 at 18:47
Ya that's my roadblock: don't know what the type of that method would be :(
– Adrian
Nov 20 '18 at 18:55
Ya that's my roadblock: don't know what the type of that method would be :(
– Adrian
Nov 20 '18 at 18:55
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388226%2ffunction-that-retrieves-element-from-hlist-while-preserving-its-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
That's not the correct code to reproduce the result you describe. For one thing,
getLast()
, as posted, is not recursive.– jwvh
Nov 20 '18 at 9:00
@jwvh good point - I wrote this late at night (my time) and initially was thinking of a recursive fn. I did mean to wrap
init(hl)
intogetLast(init(hl))
– Adrian
Nov 20 '18 at 17:47