Hi Silviu,

On Sunday, July 23, 2017 at 2:11:29 AM UTC+2, Silviu Capota Mera wrote:
>
> Hi Sofiane,
>
> "Is my design wrong?"
> Without a bigger picture of what your final aim is, it's hard for an 
> external observer to tell you if your design is right or wrong.
> I was unable to fully grasp the need for that intermediate (CSV prefixed) 
> structs in-between raw csv and real domain types, so I just assumed you're 
> working on some sort of a streaming processor / data importer of sorts 
> where values were represented by basic types (strings, numbers, etc).
>

Yes , the idea is to do streaming of incoming rows. Working with CSVxxx, 
which are mainly custom wrapper of basic types, allows me to take advantage 
of go interfaces to add behavior. I'm not sure now if it's the right 
approach.
 

> "Is your example and/or Roger's the most idiomatic way to do it in Go?" 
> I think Roger's and my example were targeted towards helping you figure 
> out a specific problem you had: supplying custom functionality to that 
> transformer of yours, that you can apply as needed on the original values. 
> He used a type switch and I used a type assertion. A third way (which we 
> did not cover, but he mentioned) is using reflection (the reflect package).
> No matter what, using your design with the Valuer interface, you need to 
> explicitly code the type check and get the concrete type, in order to do 
> different things to a string versus a float.
>
> "Is it wrong to use type assertions in Go?"
> Type assertions are a fundamental part of Go. There's nothing wrong using 
> them: https://tour.golang.org/methods/15 
>

Clearly I have a lot of things to learn, I'll have a look at type 
assertions and switched. 

>
> Regarding "idiomatic": Maybe it's just that you are trying to drive the Go 
> vehicle using a Scala driver's licence, and you need more time to get used 
> to what can and what cannot be done ? 
> I would urge you to keep at it, try several variations, and in 2-3 months 
> tops you'll be able to naturally calibrate your perspective, and tell which 
> code feels idiomatic and which doesn't.
>

I guess yes I'm biased :)
 

>
> Cheers,
> silviu
>
> On Saturday, 22 July 2017 13:46:09 UTC-4, Sofiane Cherchalli wrote:
>>
>> Hi Silviu,
>>
>> Thanks for the example.
>>
>> I have some questions popping up in my mind: Is my design wrong? Is your 
>> example and/or Roger's the most idiomatic way to do it in Go?
>>
>> Is it wrong to use type assertions in Go?
>>
>> Thanks.
>>
>> On Thursday, July 20, 2017 at 6:32:47 PM UTC+2, Silviu Capota Mera wrote:
>>>
>>> Hi Sofiane,
>>>
>>> Answering the "what type" question is pretty much unavoidable. 
>>> You can embed that forking logic inside the "on the fly" function, like 
>>> in Roger's example (using a switch v := v.(type) construct) or you can use 
>>> reflect.
>>>
>>> Alternatively, you can group your transformation functions into 
>>> functionality buckets, using maps: map[string]TFunc (where TFunc is 
>>> func(Valuer) Valuer like you wanted).
>>> Each of your types (CSVString or CSVFloat, etc) would be "tied" to such 
>>> a map, returned by a new method in the Valuer interface: TFuncs() 
>>> map[string]TFunc
>>> Quick example here:  https://play.golang.org/p/ZnqJVw0Wzq
>>> I just added some extra stuff to your code. I didn't bother trying to 
>>> understand if you wanted your rows mutated for each operation, or copied, 
>>> or such. That's your decision.
>>>
>>> For chaining transformers, please note that I changed your r := Row { 
>>> ... }  to var r Transformer = &Row{ ... }
>>>
>>> Keep in mind that a lot of functional paradigms do not apply cleanly to 
>>> Go. For readability purposes, a simple for loop works wonders and it's 
>>> oftentimes more readable :)
>>>
>>> cheers,
>>> silviu
>>>
>>>
>>>
>>>
>>> On Thursday, 20 July 2017 07:58:09 UTC-4, rog wrote:
>>>>
>>>> I'm not convinced that holding all your values in a uniform way
>>>> is going to be that helpful for you. You might be better using 
>>>> reflection
>>>> to map the columns into a struct type (there's probably a package
>>>> out there already that can do that).
>>>>
>>>> However, to answer without questioning the whole premise:
>>>>
>>>> You can't pass a function on a specific type to the more generally
>>>> typed func(Valuer)Valuer because the value in the column might not
>>>> be of the specific type - what should happen if the column is a string
>>>> and you pass func(CSVFloat)CSVFloat to Apply?
>>>>
>>>> Here's your code made to work, with some arguably redundant stuff
>>>> removed. The Transformer type seemed unnecessary, as Apply
>>>> and RemoveColumn both work with the row in place. The Type field
>>>> in the Column struct seemed unnecessary, as the type is implied by
>>>> the value in the column. Also, the whole notion of Type seemed
>>>> a bit redundant as CSV files have no notion of type, and it seems like
>>>> you want to support custom types. The CSV prefix on the type names
>>>> seemed unnecessary, as this would probably be in a package with
>>>> some kind of csv-related name.
>>>>
>>>> https://play.golang.org/p/9mSfG1m4VZ
>>>>
>>>>   cheers,
>>>>     rog.
>>>>
>>>> On 20 July 2017 at 08:05, Sofiane Cherchalli <sofi...@gmail.com> wrote:
>>>>
>>>>> Hi Silviu,
>>>>>
>>>>> Thanks for the reply.
>>>>>
>>>>> Basically I want to kinda functional map on my custom types by 
>>>>> applying functions on base value or struct values.
>>>>>
>>>>> What if I want to for instance:
>>>>>
>>>>> - Multiply the float64 value inside CSVFloat by 2 ?
>>>>> - or Replace a custom type value with another one from the same type?
>>>>>
>>>>> Thanks
>>>>>
>>>>>
>>>>> On Thursday, July 20, 2017 at 5:09:40 AM UTC+2, Silviu Capota Mera 
>>>>> wrote:
>>>>>>
>>>>>> Before: myfn := func(v CSVFloat) CSVFloat { return v }
>>>>>>
>>>>>> After: myfn := func(v Valuer) Valuer { return v }
>>>>>>
>>>>>> On Wednesday, 19 July 2017 16:48:07 UTC-4, Sofiane Cherchalli wrote:
>>>>>>>
>>>>>>> Hi!
>>>>>>>
>>>>>>> I'm a noob in Go and I need some guidance/help on this: 
>>>>>>> https://play.golang.org/p/0TGzKiYQZn
>>>>>>>
>>>>>>> Basically I'm implementing a CSV parser, and applying 
>>>>>>> transformations on column value.
>>>>>>>
>>>>>>> In last part of the code I'm trying to apply a function on CSVFloat 
>>>>>>> type which satisfies Valuer interface, but I got a compiler error.
>>>>>>>
>>>>>>> In Scala language, this could be done by using map function, but how 
>>>>>>> to do it in Golang?
>>>>>>>
>>>>>>> Thanks.
>>>>>>>
>>>>>> -- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "golang-nuts" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>>> an email to golang-nuts...@googlegroups.com.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to