• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 14:04
CEST 20:04
KST 03:04
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL21] Ro24 Preview Pt2: News Flash6[ASL21] Ro24 Preview Pt1: New Chaos0Team Liquid Map Contest #22 - Presented by Monster Energy11ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book20
Community News
Weekly Cups (March 16-22): herO doubles, Cure surprises3Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool49Weekly Cups (March 9-15): herO, Clem, ByuN win42026 KungFu Cup Announcement6BGE Stara Zagora 2026 cancelled12
StarCraft 2
General
What mix of new & old maps do you want in the next ladder pool? (SC2) Team Liquid Map Contest #22 - Presented by Monster Energy herO wins SC2 All-Star Invitational Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Potential Updates Coming to the SC2 CN Server
Tourneys
RSL Season 4 announced for March-April Sparkling Tuna Cup - Weekly Open Tournament StarCraft Evolution League (SC Evo Biweekly) WardiTV Mondays World University TeamLeague (500$+) | Signups Open
Strategy
Custom Maps
[M] (2) Frigid Storage Publishing has been re-enabled! [Feb 24th 2026]
External Content
Mutation # 519 Inner Power The PondCast: SC2 News & Results Mutation # 518 Radiation Zone Mutation # 517 Distant Threat
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ [ASL21] Ro24 Preview Pt2: News Flash Pros React To: SoulKey vs Ample ASL21 General Discussion RepMastered™: replay sharing and analyzer site
Tourneys
[ASL21] Ro24 Group D [Megathread] Daily Proleagues [ASL21] Ro24 Group C [ASL21] Ro24 Group B
Strategy
What's the deal with APM & what's its true value Fighting Spirit mining rates Simple Questions, Simple Answers
Other Games
General Games
Nintendo Switch Thread Starcraft Tabletop Miniature Game General RTS Discussion Thread Stormgate/Frost Giant Megathread Darkest Dungeon
Dota 2
The Story of Wings Gaming Official 'what is Dota anymore' discussion
League of Legends
G2 just beat GenG in First stand
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Five o'clock TL Mafia Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine The Games Industry And ATVI European Politico-economics QA Mega-thread Canadian Politics Mega-thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Manga] One Piece [Req][Books] Good Fantasy/SciFi books Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion Cricket [SPORT] Tokyo Olympics 2021 Thread General nutrition recommendations
World Cup 2022
Tech Support
[G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Funny Nicknames
LUCKY_NOOB
Money Laundering In Video Ga…
TrAiDoS
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1654 users

The Big Programming Thread - Page 907

Forum Index > General Forum
Post a Reply
Prev 1 905 906 907 908 909 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Manit0u
Profile Blog Joined August 2004
Poland17706 Posts
September 22 2017 06:55 GMT
#18121
On September 22 2017 12:54 NovemberstOrm wrote:
Anyone familiar with Moment JS? And knows how to parse dates/time pulled from an API(json format) into only hours/minutes?


https://momentjs.com/docs/#/parsing/string-format/

At the bottom of the section:

moment("123", "hmm").format("HH:mm") === "01:23"
moment("1234", "hmm").format("HH:mm") === "12:34"
Time is precious. Waste it wisely.
NovemberstOrm
Profile Blog Joined September 2011
Canada16217 Posts
September 22 2017 22:55 GMT
#18122
On September 22 2017 15:55 Manit0u wrote:
Show nested quote +
On September 22 2017 12:54 NovemberstOrm wrote:
Anyone familiar with Moment JS? And knows how to parse dates/time pulled from an API(json format) into only hours/minutes?


https://momentjs.com/docs/#/parsing/string-format/

At the bottom of the section:

moment("123", "hmm").format("HH:mm") === "01:23"
moment("1234", "hmm").format("HH:mm") === "12:34"

thanks worked great!
Moderatorlickypiddy
dekibeki
Profile Joined June 2011
Australia34 Posts
September 23 2017 02:41 GMT
#18123
On September 21 2017 08:56 Manit0u wrote:
Guys, I need a bit of help with conceptual work. Maybe some of you have had any experience with something like that before...

Basically it's all about process pipelining. We create the configuration and then each process step (step in the pipeline) receives some input files and produces some output files. The problem I'm facing at the moment is that different processes require different file and I need to map the correlations.

Example:

Process A has input files a, b, c and produces output files d, e.
Process B has input files f, g and produces output file h.

All the inputs/outputs need to be unique (it matters which one is which, don't ask me why) so when I go to the next step in the pipeline (process A finished) I need to know that I have to pass output d as input g and output e as input f so that process B can start.

I can't discover/calculate this on the fly since process configuration might change but all of the old jobs (series of steps) have to retain their old configuration.

Now. What do you think would be the best way to map such configurations in the database (postgres) so that it isn't too clunky to retrieve/parse?

The only thing I came up with this far is some form of config table, which will be attached to the process (one process has many configs and each config specifies the relation between output of this process and input of the next one) but this seems super fragile...


Assuming each file has only 1 producer and consumer, and that files may be left un-produced (user supplied) or un-consumed (user-consumed)

Using a relational db,

+ Show Spoiler +



CREATE TABLE Processes (
id INT NOT NULL,
name VARCHAR[64] NOT NULL,
CONSTRAINT pk_processes PRIMARY KEY (id));

CREATE TABLE Files (
id INT NOT NULL,
name VARCHAR[64] NOT NULL,
producer INT,
consumer INT,
CONSTRAINT pk_files PRIMARY KEY (id),
CONSTRAINT fk_files__producer__processes__id FOREIGN KEY (producer) REFERENCES Processes(id),
CONSTRAINT fk_files__consumer__processes_id FOREIGN KEY (consumer) REFERENCES Processes(id));




If files may have multiple consumers, move consumer out of Files and into something like:
+ Show Spoiler +



CREATE TABLE File_consumers (
id INT NOT NULL,
file INT NOT NULL,
consumer INT NOT NULL,
CONSTRAINT pk_file_consumers PRIMARY KEY(id),
CONSTRAINT fk_file_consumers__file__files__id FOREIGN KEY (file) REFERENCES Files(id),
CONSTRAINT fk_file_consumers__consumer__processes_id FOREIGN KEY (consumer) REFERENCES Processes(id));




This unfortunately allows for no consumers, but I'm assuming your pipeline ends somewhere. You should use whatever db specific id type your db has for the id/primary keys. For pgsql I believe its UUID.
Thaniri
Profile Blog Joined March 2011
1264 Posts
September 24 2017 05:38 GMT
#18124
Anyone play with golang as a scripting language?

I've begun an internship this month and my task is to work with a team of 3 people to redesign and rebuild the way the company does builds and deployments of their application. Right now the build/deploy system works but is a hard to maintain behemoth of bash scripts that kick off other bash scripts being called by teamcity and jenkins. At least it's version controlled.

I want to pitch to my team the idea of using golang for rewriting all of these infrastructure scripts. Basically because it is strongly and statically typed I believe that it is a more maintainable scripting language compared to bash or python.

Points of resistance: no-one else seems to know golang, and I'm a fucking interrn.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
September 24 2017 18:35 GMT
#18125
I have to write an ocaml function that takes 2 lists and returns a list of tuples, where each tuple is a pair of elements - 1 from each list

for example [1. 2. 3] [5,6] would return [(1,5);(1.6),(2,5);(2.6),(3,5);(3.6)]

Here's the kicker. We're supposed to be able to do it without using recursion, and without using helper methods other than fold methods, map method, append method (combine 2 lists), and length method (length of list)


function signature is:

let pairs lst1 lst2 =



I solved it by doing:


let rec pairs lst1 lst2 = match lst1 with
[] -> []
| h::t -> append (fold_right (fun x a -> (h, x)::a) lst2 []) (pairs t lst2)


And it says I will get full points for that. But I am supposed to be able to solve this without recusion and I want to know how.



IMPORTANT EDIT:

Apparently ocaml doesn't treat this function as recursive. I can get rid of the "rec" in my function signature and everything works fine. Why is this not considered recursive? Is it because it's tail recursive?
Liebig
Profile Joined August 2010
France738 Posts
Last Edited: 2017-09-24 18:51:44
September 24 2017 18:41 GMT
#18126
On September 25 2017 03:35 travis wrote:
I have to write an ocaml function that takes 2 lists and returns a list of tuples, where each tuple is a pair of elements - 1 from each list

for example [1. 2. 3] [5,6] would return [(1,5);(1.6),(2,5);(2.6),(3,5);(3.6)]

Here's the kicker. We're supposed to be able to do it without using recursion, and without using helper methods other than fold methods, map method, append method (combine 2 lists), and length method (length of list)


function signature is:

let pairs lst1 lst2 =



I solved it by doing:


let rec pairs lst1 lst2 = match lst1 with
[] -> []
| h::t -> append (fold_right (fun x a -> (h, x)::a) lst2 []) (pairs t lst2)


And it says I will get full points for that. But I am supposed to be able to solve this without recusion and I want to know how.



IMPORTANT EDIT:

Apparently ocaml doesn't treat this function as recursive. I can get rid of the "rec" in my function signature and everything works fine. Why is this not considered recursive? Is it because it's tail recursive?


This is a recursive function, i imagine you are testing in a REPL, try restarting from scratch and remove the rec, it should reject it. And it is not tail-recursive.


As for a solution, i think this should work


let pairs l1 l 2 = List.map (fun x -> List.map (fun y -> (x, y)) l2) l1 |> List.flatten


Poirier255
Profile Joined July 2013
Canada2 Posts
September 24 2017 18:48 GMT
#18127
On September 24 2017 14:38 Thaniri wrote:
Anyone play with golang as a scripting language?

I've begun an internship this month and my task is to work with a team of 3 people to redesign and rebuild the way the company does builds and deployments of their application. Right now the build/deploy system works but is a hard to maintain behemoth of bash scripts that kick off other bash scripts being called by teamcity and jenkins. At least it's version controlled.

I want to pitch to my team the idea of using golang for rewriting all of these infrastructure scripts. Basically because it is strongly and statically typed I believe that it is a more maintainable scripting language compared to bash or python.

Points of resistance: no-one else seems to know golang, and I'm a fucking interrn.


It depends.

Did you completely inherit this legacy code or are you working with folks who know this stuff and have years of experience working with the system?

If it's completely yours; and you're confident enough that you can rewrite all of this already set-in-stone, tested, tried-and-true set of scripts, then do it. You have all the power, just remember to use it wisely.

However if you're an intern in an age-old company that just knows how its stuff roles on a day to day basis, anything you screw up is going to cause panic, and nobody will like you for that. Don't rewrite something for the sake of complexity unless you work with it on a daily basis and feel that a rewrite would GREATLY (serious emphasis on this word) improve your productivity. Otherwise, it's there, and it works; don't change it for the sake of complexity (it's really not that bad, as much as you might want to believe it is).

I don't know how critical your system is, but typically you don't want to fix something that isn't broken; especially if you're just trying to look good to your managers so you can have that job at the end of your internship.

Don't look at a company using old technology as an "Omg they need to catch up!" type of deal. A company is there to make money. If the system that's in place is bringing in the dough, you want to be very careful when you're tasked with tampering with that system.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-09-24 19:19:36
September 24 2017 19:05 GMT
#18128
On September 25 2017 03:41 Liebig wrote:
Show nested quote +
On September 25 2017 03:35 travis wrote:
I have to write an ocaml function that takes 2 lists and returns a list of tuples, where each tuple is a pair of elements - 1 from each list

for example [1. 2. 3] [5,6] would return [(1,5);(1.6),(2,5);(2.6),(3,5);(3.6)]

Here's the kicker. We're supposed to be able to do it without using recursion, and without using helper methods other than fold methods, map method, append method (combine 2 lists), and length method (length of list)


function signature is:

let pairs lst1 lst2 =



I solved it by doing:


let rec pairs lst1 lst2 = match lst1 with
[] -> []
| h::t -> append (fold_right (fun x a -> (h, x)::a) lst2 []) (pairs t lst2)


And it says I will get full points for that. But I am supposed to be able to solve this without recusion and I want to know how.



IMPORTANT EDIT:

Apparently ocaml doesn't treat this function as recursive. I can get rid of the "rec" in my function signature and everything works fine. Why is this not considered recursive? Is it because it's tail recursive?


This is a recursive function, i imagine you are testing in a REPL, try restarting from scratch and remove the rec, it should reject it

As for a solution, i think this should work


let pairs l1 l 2 = List.map (fun x -> List.map (fun y -> (x, y)) l2) l1 |> List.flatten




ah you're right, that must be it. I am using a repl

as for your solution, I am not allowed to use the list module. only pervasives, and the functions I listed (fold_left, fold_right, map, append, length). But I suppose that flatten is basically the append we have given to use.


And so I am trying to understand what you are doing
So this maps each l1 element x to each l2 element y in the form (x, y)

It will work like that? really cool, I'll check it out

|> is like a unix pipe? So that won't work for me here unfortunately. How can I use append for each list that is made by the outside List.map ?

I suppose I could do a fold on the list of lists and do an append for each list to combine them ?


edit: got it working. no recursion. very cool, thank you.
Liebig
Profile Joined August 2010
France738 Posts
September 24 2017 19:26 GMT
#18129
On September 25 2017 04:05 travis wrote:
Show nested quote +
On September 25 2017 03:41 Liebig wrote:
On September 25 2017 03:35 travis wrote:
I have to write an ocaml function that takes 2 lists and returns a list of tuples, where each tuple is a pair of elements - 1 from each list

for example [1. 2. 3] [5,6] would return [(1,5);(1.6),(2,5);(2.6),(3,5);(3.6)]

Here's the kicker. We're supposed to be able to do it without using recursion, and without using helper methods other than fold methods, map method, append method (combine 2 lists), and length method (length of list)


function signature is:

let pairs lst1 lst2 =



I solved it by doing:


let rec pairs lst1 lst2 = match lst1 with
[] -> []
| h::t -> append (fold_right (fun x a -> (h, x)::a) lst2 []) (pairs t lst2)


And it says I will get full points for that. But I am supposed to be able to solve this without recusion and I want to know how.



IMPORTANT EDIT:

Apparently ocaml doesn't treat this function as recursive. I can get rid of the "rec" in my function signature and everything works fine. Why is this not considered recursive? Is it because it's tail recursive?


This is a recursive function, i imagine you are testing in a REPL, try restarting from scratch and remove the rec, it should reject it

As for a solution, i think this should work


let pairs l1 l 2 = List.map (fun x -> List.map (fun y -> (x, y)) l2) l1 |> List.flatten




ah you're right, that must be it. I am using a repl

as for your solution, I am not allowed to use the list module. only pervasives, and the functions I listed (fold_left, fold_right, map, append, length). But I suppose that flatten is basically the append we have given to use.


And so I am trying to understand what you are doing
So this maps each l1 element x to each l2 element y in the form (x, y)

It will work like that? really cool, I'll check it out

|> is like a unix pipe? So that won't work for me here unfortunately. How can I use append for each list that is made by the outside List.map ?

I suppose I could do a fold on the list of lists and do an append for each list to combine them ?


edit: got it working. no recursion. very cool, thank you.


Yes I'm basically building a list [(x,y1);(x,y2),...] for each x in l1 and y1, y2,... in l2
So you get a list of lists, and the result you want is the one where you "flatten" the list.

|> is defined as let (|>) x f = f x. So this is basically syntactic sugar to say take everything before |> and give it as argument to the function that is after.

I think flatten can be defined as fun x -> fold_right append x []

Thaniri
Profile Blog Joined March 2011
1264 Posts
September 24 2017 21:02 GMT
#18130
On September 25 2017 03:48 Poirier255 wrote:
Show nested quote +
On September 24 2017 14:38 Thaniri wrote:
Anyone play with golang as a scripting language?

I've begun an internship this month and my task is to work with a team of 3 people to redesign and rebuild the way the company does builds and deployments of their application. Right now the build/deploy system works but is a hard to maintain behemoth of bash scripts that kick off other bash scripts being called by teamcity and jenkins. At least it's version controlled.

I want to pitch to my team the idea of using golang for rewriting all of these infrastructure scripts. Basically because it is strongly and statically typed I believe that it is a more maintainable scripting language compared to bash or python.

Points of resistance: no-one else seems to know golang, and I'm a fucking interrn.


It depends.

Did you completely inherit this legacy code or are you working with folks who know this stuff and have years of experience working with the system?

If it's completely yours; and you're confident enough that you can rewrite all of this already set-in-stone, tested, tried-and-true set of scripts, then do it. You have all the power, just remember to use it wisely.

However if you're an intern in an age-old company that just knows how its stuff roles on a day to day basis, anything you screw up is going to cause panic, and nobody will like you for that. Don't rewrite something for the sake of complexity unless you work with it on a daily basis and feel that a rewrite would GREATLY (serious emphasis on this word) improve your productivity. Otherwise, it's there, and it works; don't change it for the sake of complexity (it's really not that bad, as much as you might want to believe it is).

I don't know how critical your system is, but typically you don't want to fix something that isn't broken; especially if you're just trying to look good to your managers so you can have that job at the end of your internship.

Don't look at a company using old technology as an "Omg they need to catch up!" type of deal. A company is there to make money. If the system that's in place is bringing in the dough, you want to be very careful when you're tasked with tampering with that system.


The way builds are done at the company is a recognized problem. The amount of fixing is what is up for debate. Some say complete rewrite, some say refactor. The debate will hopefully be settled soon. The team lead wants golang, junior wants python, original guys want to stay on bash or MAYBE go to ruby. As an intern, my opinion won't really matter all that much I don't think but, given that it is potentially quite a large project, I think that golang is a good option.

For instance I'm going to demo a script I built in golang that updates all of our alerts to ping the correct domains after a blue/green deployment. Currently that is a manual process, and my script can potentially make it a one click or even completely automated process. I could have written it in bash just as well but just wanted to see how golang works.
berated-
Profile Blog Joined February 2007
United States1134 Posts
Last Edited: 2017-09-24 22:47:08
September 24 2017 22:45 GMT
#18131
On September 25 2017 06:02 Thaniri wrote:
Show nested quote +
On September 25 2017 03:48 Poirier255 wrote:
On September 24 2017 14:38 Thaniri wrote:
Anyone play with golang as a scripting language?

I've begun an internship this month and my task is to work with a team of 3 people to redesign and rebuild the way the company does builds and deployments of their application. Right now the build/deploy system works but is a hard to maintain behemoth of bash scripts that kick off other bash scripts being called by teamcity and jenkins. At least it's version controlled.

I want to pitch to my team the idea of using golang for rewriting all of these infrastructure scripts. Basically because it is strongly and statically typed I believe that it is a more maintainable scripting language compared to bash or python.

Points of resistance: no-one else seems to know golang, and I'm a fucking interrn.


It depends.

Did you completely inherit this legacy code or are you working with folks who know this stuff and have years of experience working with the system?

If it's completely yours; and you're confident enough that you can rewrite all of this already set-in-stone, tested, tried-and-true set of scripts, then do it. You have all the power, just remember to use it wisely.

However if you're an intern in an age-old company that just knows how its stuff roles on a day to day basis, anything you screw up is going to cause panic, and nobody will like you for that. Don't rewrite something for the sake of complexity unless you work with it on a daily basis and feel that a rewrite would GREATLY (serious emphasis on this word) improve your productivity. Otherwise, it's there, and it works; don't change it for the sake of complexity (it's really not that bad, as much as you might want to believe it is).

I don't know how critical your system is, but typically you don't want to fix something that isn't broken; especially if you're just trying to look good to your managers so you can have that job at the end of your internship.

Don't look at a company using old technology as an "Omg they need to catch up!" type of deal. A company is there to make money. If the system that's in place is bringing in the dough, you want to be very careful when you're tasked with tampering with that system.


The way builds are done at the company is a recognized problem. The amount of fixing is what is up for debate. Some say complete rewrite, some say refactor. The debate will hopefully be settled soon. The team lead wants golang, junior wants python, original guys want to stay on bash or MAYBE go to ruby. As an intern, my opinion won't really matter all that much I don't think but, given that it is potentially quite a large project, I think that golang is a good option.

For instance I'm going to demo a script I built in golang that updates all of our alerts to ping the correct domains after a blue/green deployment. Currently that is a manual process, and my script can potentially make it a one click or even completely automated process. I could have written it in bash just as well but just wanted to see how golang works.


What type of software are you deploying? To me trading off bash scripts for go lang doesn't seem like an upgrade at all -- just seems like you're rewriting for rewrite sake. Is it not possible to consider things that are becoming more standard like docker or ansible?

Edit: understand that docker isn't really a solution as it needs orchestration, but.. more just keep thinking that custom is custom - don't see a big difference between any language.
Thaniri
Profile Blog Joined March 2011
1264 Posts
September 24 2017 23:09 GMT
#18132
To prevent the risk of misspeaking about our situation, I'll accept that I shouldn't push golang just cause I think it's cool.

To explain why we need to change our build system as simply as possible: there are different deployment procedures for different environments and that's a pain in the ass. We want to consolidate it into a single common procedure for every environment.

Orchestration is no problem for now, we use Puppet for system configurations. Ansible has been pitched as well. When I say deployment I mean simply the software releases that go out to every environment. Not tearing down and rebuilding the VMs running the software.
Chocolate
Profile Blog Joined December 2010
United States2350 Posts
September 24 2017 23:40 GMT
#18133
Can someone please tell me why the FUCK pandas is as popular as it is? So far in my life I have not once encountered pandas being used in any situation without there being an obvious better solution. It seems to basically just be a tool for people coming from R/Excel backgrounds to organize their data in a familiar manner in Python. But it has probably the worst syntax/design out of any popular Python modules and almost all of the things it makes "easy" can be done in one or two lines anyway.
Manit0u
Profile Blog Joined August 2004
Poland17706 Posts
Last Edited: 2017-09-25 00:52:29
September 25 2017 00:46 GMT
#18134
On September 25 2017 08:09 Thaniri wrote:
To prevent the risk of misspeaking about our situation, I'll accept that I shouldn't push golang just cause I think it's cool.

To explain why we need to change our build system as simply as possible: there are different deployment procedures for different environments and that's a pain in the ass. We want to consolidate it into a single common procedure for every environment.

Orchestration is no problem for now, we use Puppet for system configurations. Ansible has been pitched as well. When I say deployment I mean simply the software releases that go out to every environment. Not tearing down and rebuilding the VMs running the software.


If you want to rewrite/refactor your scripts I really hope you have BATS (or something like it) in there too. If not, I think that's the first thing you should look up to before moving anywhere with this...

Also, if you want to rewrite it in something other than bash, I'd look into Crystal. We've had a lot of success with it in our company and it's becoming our go-to language for various scripting tasks.
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18247 Posts
Last Edited: 2017-09-25 11:23:38
September 25 2017 11:20 GMT
#18135
On September 25 2017 04:26 Liebig wrote:
Show nested quote +
On September 25 2017 04:05 travis wrote:
On September 25 2017 03:41 Liebig wrote:
On September 25 2017 03:35 travis wrote:
I have to write an ocaml function that takes 2 lists and returns a list of tuples, where each tuple is a pair of elements - 1 from each list

for example [1. 2. 3] [5,6] would return [(1,5);(1.6),(2,5);(2.6),(3,5);(3.6)]

Here's the kicker. We're supposed to be able to do it without using recursion, and without using helper methods other than fold methods, map method, append method (combine 2 lists), and length method (length of list)


function signature is:

let pairs lst1 lst2 =



I solved it by doing:


let rec pairs lst1 lst2 = match lst1 with
[] -> []
| h::t -> append (fold_right (fun x a -> (h, x)::a) lst2 [] (pairs t lst2)


And it says I will get full points for that. But I am supposed to be able to solve this without recusion and I want to know how.



IMPORTANT EDIT:

Apparently ocaml doesn't treat this function as recursive. I can get rid of the "rec" in my function signature and everything works fine. Why is this not considered recursive? Is it because it's tail recursive?


This is a recursive function, i imagine you are testing in a REPL, try restarting from scratch and remove the rec, it should reject it

As for a solution, i think this should work


let pairs l1 l 2 = List.map (fun x -> List.map (fun y -> (x, y)) l2) l1 |> List.flatten




ah you're right, that must be it. I am using a repl

as for your solution, I am not allowed to use the list module. only pervasives, and the functions I listed (fold_left, fold_right, map, append, length). But I suppose that flatten is basically the append we have given to use.


And so I am trying to understand what you are doing
So this maps each l1 element x to each l2 element y in the form (x, y)

It will work like that? really cool, I'll check it out

|> is like a unix pipe? So that won't work for me here unfortunately. How can I use append for each list that is made by the outside List.map ?

I suppose I could do a fold on the list of lists and do an append for each list to combine them ?


edit: got it working. no recursion. very cool, thank you.


Yes I'm basically building a list [(x,y1);(x,y2),...] for each x in l1 and y1, y2,... in l2
So you get a list of lists, and the result you want is the one where you "flatten" the list.

|> is defined as let (|>) x f = f x. So this is basically syntactic sugar to say take everything before |> and give it as argument to the function that is after.

I think flatten can be defined as fun x -> fold_right append x []



Looks right to me. It's a restricted version of flatten, but it's perfect in this case. I can't comment on syntax, but my knowledge of foldr.

If you can't use syntactic sugar for chaining together functions just write it in order. It's uglier and far harder to understand, but there's no functionality lost.

If all the code above is valid ocaml, I have to say it looks very similar to Haskell. I'll have to look into what the actual differences are between the two, because the core functional programming looks almost identical (with a slightly different syntax). Does ocaml have monads?

E: Google answered. Yes, ocaml has monads. Insofar as I can see, the type system is the main difference, and some of the plumbing.
https://www.quora.com/Which-of-Haskell-and-OCaml-is-more-practical-For-example-in-which-aspect-will-each-play-a-key-role
Liebig
Profile Joined August 2010
France738 Posts
September 25 2017 13:17 GMT
#18136
On September 25 2017 20:20 Acrofales wrote:
Show nested quote +
On September 25 2017 04:26 Liebig wrote:
On September 25 2017 04:05 travis wrote:
On September 25 2017 03:41 Liebig wrote:
On September 25 2017 03:35 travis wrote:
I have to write an ocaml function that takes 2 lists and returns a list of tuples, where each tuple is a pair of elements - 1 from each list

for example [1. 2. 3] [5,6] would return [(1,5);(1.6),(2,5);(2.6),(3,5);(3.6)]

Here's the kicker. We're supposed to be able to do it without using recursion, and without using helper methods other than fold methods, map method, append method (combine 2 lists), and length method (length of list)


function signature is:

let pairs lst1 lst2 =



I solved it by doing:


let rec pairs lst1 lst2 = match lst1 with
[] -> []
| h::t -> append (fold_right (fun x a -> (h, x)::a) lst2 [] (pairs t lst2)


And it says I will get full points for that. But I am supposed to be able to solve this without recusion and I want to know how.



IMPORTANT EDIT:

Apparently ocaml doesn't treat this function as recursive. I can get rid of the "rec" in my function signature and everything works fine. Why is this not considered recursive? Is it because it's tail recursive?


This is a recursive function, i imagine you are testing in a REPL, try restarting from scratch and remove the rec, it should reject it

As for a solution, i think this should work


let pairs l1 l 2 = List.map (fun x -> List.map (fun y -> (x, y)) l2) l1 |> List.flatten




ah you're right, that must be it. I am using a repl

as for your solution, I am not allowed to use the list module. only pervasives, and the functions I listed (fold_left, fold_right, map, append, length). But I suppose that flatten is basically the append we have given to use.


And so I am trying to understand what you are doing
So this maps each l1 element x to each l2 element y in the form (x, y)

It will work like that? really cool, I'll check it out

|> is like a unix pipe? So that won't work for me here unfortunately. How can I use append for each list that is made by the outside List.map ?

I suppose I could do a fold on the list of lists and do an append for each list to combine them ?


edit: got it working. no recursion. very cool, thank you.


Yes I'm basically building a list [(x,y1);(x,y2),...] for each x in l1 and y1, y2,... in l2
So you get a list of lists, and the result you want is the one where you "flatten" the list.

|> is defined as let (|>) x f = f x. So this is basically syntactic sugar to say take everything before |> and give it as argument to the function that is after.

I think flatten can be defined as fun x -> fold_right append x []



Looks right to me. It's a restricted version of flatten, but it's perfect in this case. I can't comment on syntax, but my knowledge of foldr.

If you can't use syntactic sugar for chaining together functions just write it in order. It's uglier and far harder to understand, but there's no functionality lost.

If all the code above is valid ocaml, I have to say it looks very similar to Haskell. I'll have to look into what the actual differences are between the two, because the core functional programming looks almost identical (with a slightly different syntax). Does ocaml have monads?

E: Google answered. Yes, ocaml has monads. Insofar as I can see, the type system is the main difference, and some of the plumbing.
https://www.quora.com/Which-of-Haskell-and-OCaml-is-more-practical-For-example-in-which-aspect-will-each-play-a-key-role

One big difference between Haskell and OCaml is that one is call by name, while the other is call by value.
ZigguratOfUr
Profile Blog Joined April 2012
Iraq16955 Posts
September 25 2017 22:51 GMT
#18137
On September 25 2017 22:17 Liebig wrote:
Show nested quote +
On September 25 2017 20:20 Acrofales wrote:
On September 25 2017 04:26 Liebig wrote:
On September 25 2017 04:05 travis wrote:
On September 25 2017 03:41 Liebig wrote:
On September 25 2017 03:35 travis wrote:
I have to write an ocaml function that takes 2 lists and returns a list of tuples, where each tuple is a pair of elements - 1 from each list

for example [1. 2. 3] [5,6] would return [(1,5);(1.6),(2,5);(2.6),(3,5);(3.6)]

Here's the kicker. We're supposed to be able to do it without using recursion, and without using helper methods other than fold methods, map method, append method (combine 2 lists), and length method (length of list)


function signature is:

let pairs lst1 lst2 =



I solved it by doing:


let rec pairs lst1 lst2 = match lst1 with
[] -> []
| h::t -> append (fold_right (fun x a -> (h, x)::a) lst2 [] (pairs t lst2)


And it says I will get full points for that. But I am supposed to be able to solve this without recusion and I want to know how.



IMPORTANT EDIT:

Apparently ocaml doesn't treat this function as recursive. I can get rid of the "rec" in my function signature and everything works fine. Why is this not considered recursive? Is it because it's tail recursive?


This is a recursive function, i imagine you are testing in a REPL, try restarting from scratch and remove the rec, it should reject it

As for a solution, i think this should work


let pairs l1 l 2 = List.map (fun x -> List.map (fun y -> (x, y)) l2) l1 |> List.flatten




ah you're right, that must be it. I am using a repl

as for your solution, I am not allowed to use the list module. only pervasives, and the functions I listed (fold_left, fold_right, map, append, length). But I suppose that flatten is basically the append we have given to use.


And so I am trying to understand what you are doing
So this maps each l1 element x to each l2 element y in the form (x, y)

It will work like that? really cool, I'll check it out

|> is like a unix pipe? So that won't work for me here unfortunately. How can I use append for each list that is made by the outside List.map ?

I suppose I could do a fold on the list of lists and do an append for each list to combine them ?


edit: got it working. no recursion. very cool, thank you.


Yes I'm basically building a list [(x,y1);(x,y2),...] for each x in l1 and y1, y2,... in l2
So you get a list of lists, and the result you want is the one where you "flatten" the list.

|> is defined as let (|>) x f = f x. So this is basically syntactic sugar to say take everything before |> and give it as argument to the function that is after.

I think flatten can be defined as fun x -> fold_right append x []



Looks right to me. It's a restricted version of flatten, but it's perfect in this case. I can't comment on syntax, but my knowledge of foldr.

If you can't use syntactic sugar for chaining together functions just write it in order. It's uglier and far harder to understand, but there's no functionality lost.

If all the code above is valid ocaml, I have to say it looks very similar to Haskell. I'll have to look into what the actual differences are between the two, because the core functional programming looks almost identical (with a slightly different syntax). Does ocaml have monads?

E: Google answered. Yes, ocaml has monads. Insofar as I can see, the type system is the main difference, and some of the plumbing.
https://www.quora.com/Which-of-Haskell-and-OCaml-is-more-practical-For-example-in-which-aspect-will-each-play-a-key-role

One big difference between Haskell and OCaml is that one is call by name, while the other is call by value.


Isn't Haskell lazily evaluated?
Liebig
Profile Joined August 2010
France738 Posts
September 26 2017 06:30 GMT
#18138
Call by name is just fancy name for lazy evaluation :p
Piledriver
Profile Blog Joined August 2010
United States1697 Posts
September 27 2017 02:41 GMT
#18139
Anyone familiar with AngularJS? Could you help debug this plunk?

plnkr.co

Basically I have an array of objects, and the user can select any object on the array, and then a particular property on the array to see its value.

This binding syntax has usually worked for me, but I'm not able to make it work for any reason today.

{{ selectedElement[selectedProperty] }}


Envy fan since NTH.
Manit0u
Profile Blog Joined August 2004
Poland17706 Posts
Last Edited: 2017-09-27 07:00:28
September 27 2017 06:58 GMT
#18140
On September 27 2017 11:41 Piledriver wrote:
Anyone familiar with AngularJS? Could you help debug this plunk?

plnkr.co

Basically I have an array of objects, and the user can select any object on the array, and then a particular property on the array to see its value.

This binding syntax has usually worked for me, but I'm not able to make it work for any reason today.

Show nested quote +
{{ selectedElement[selectedProperty] }}




Try this:


<body ng-controller="MainCtrl">
Array Element:
<select ng-model="selectedIndex">
<option ng-repeat="obj in testData.Table1" value="{{obj.Index}}">{{obj.Index}}</option>
</select>
<br>
<br>
Property Name:
<select ng-model="selectedProp">
<option ng-repeat="(k,v) in testData.Table1[0]" value="{{k}}">{{k}}</option>
</select>
<br>
<br>
Array Element[Property]
<input type='text' readonly value="{{ testData.Table1[selectedIndex][selectedProp] }}">
<br>
<br>
Selected Array Element: {{selectedIndex}}
<br>
Selected Property: {{selectedProp}}
<br>
<br>
</body>


Your selectedElement was a string. Not an object...
Time is precious. Waste it wisely.
Prev 1 905 906 907 908 909 1032 Next
Please log in or register to reply.
Live Events Refresh
Monday Night Weeklies
16:00
#46
RotterdaM977
TKL 440
IndyStarCraft 262
SteadfastSC224
BRAT_OK 155
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 977
TKL 440
IndyStarCraft 262
SteadfastSC 224
BRAT_OK 155
Hui .153
elazer 119
UpATreeSC 93
MindelVK 22
StarCraft: Brood War
Calm 3779
actioN 276
ggaemo 232
firebathero 183
Dewaltoss 95
Hyun 46
Backho 42
Shine 21
GoRush 14
yabsab 12
[ Show more ]
Bale 11
910 9
SilentControl 7
Dota 2
BananaSlamJamma357
Counter-Strike
pashabiceps2126
fl0m1589
byalli299
adren_tv40
Heroes of the Storm
Liquid`Hasu10
Other Games
Grubby1339
Beastyqt837
ceh9583
crisheroes242
KnowMe165
ProTech113
QueenE75
C9.Mang074
Trikslyr49
Organizations
Other Games
BasetradeTV222
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 20 non-featured ]
StarCraft 2
• kabyraGe 21
• Reevou 5
• intothetv
• Kozan
• sooper7s
• Migwel
• AfreecaTV YouTube
• LaughNgamezSOOP
• IndyKCrew
StarCraft: Brood War
• blackmanpl 36
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 3538
• WagamamaTV1308
League of Legends
• Jankos6927
• Nemesis2780
• TFBlade1340
Other Games
• imaqtpie848
• Shiphtur139
Upcoming Events
Replay Cast
5h 56m
Sparkling Tuna Cup
15h 56m
Afreeca Starleague
15h 56m
Rush vs PianO
Flash vs Speed
PiGosaur Cup
1d 5h
Replay Cast
1d 14h
Afreeca Starleague
1d 15h
BeSt vs Leta
Queen vs Jaedong
Replay Cast
2 days
The PondCast
2 days
OSC
3 days
RSL Revival
3 days
TriGGeR vs Cure
ByuN vs Rogue
[ Show More ]
Replay Cast
4 days
RSL Revival
4 days
Maru vs MaxPax
BSL
5 days
RSL Revival
5 days
uThermal 2v2 Circuit
5 days
BSL
6 days
Liquipedia Results

Completed

2026 Changsha Offline CUP
WardiTV Winter 2026
NationLESS Cup

Ongoing

BSL Season 22
CSL Elite League 2026
CSL Season 20: Qualifier 1
ASL Season 21
Acropolis #4 - TS6
StarCraft2 Community Team League 2026 Spring
RSL Revival: Season 4
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual

Upcoming

CSL Season 20: Qualifier 2
Escore Tournament S2: W1
CSL 2026 SPRING (S20)
Acropolis #4
IPSL Spring 2026
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
IEM Atlanta 2026
Asian Champions League 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.