tag:blogger.com,1999:blog-7225373.post3248107977167732550..comments2024-02-29T03:34:23.190-05:00Comments on Who Were the Sea Peoples?: Programmer bleggcallahhttp://www.blogger.com/profile/10065877215969589482noreply@blogger.comBlogger16125tag:blogger.com,1999:blog-7225373.post-63122957143019306022014-12-14T12:46:44.224-05:002014-12-14T12:46:44.224-05:00You did get the point that I DON'T have "...You did get the point that I DON'T have "functions," I have A function, right?gcallahhttps://www.blogger.com/profile/10065877215969589482noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-49404583644953404512014-12-14T12:24:44.118-05:002014-12-14T12:24:44.118-05:00"If you have functions ( methods, whatever) i..."If you have functions ( methods, whatever) identical except in that one respect then passing in a comparator, while better than passing a flag, is probably wrong."<br /><br />Huh? Why?<br /><br />"If not, all C descended languages and all OO languages allow passing comparators."<br /><br />A function? But that is no simpler than the 'if'.gcallahhttps://www.blogger.com/profile/10065877215969589482noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-58064508683057193122014-12-14T00:13:45.594-05:002014-12-14T00:13:45.594-05:00You won't like this answer, but from what you ...You won't like this answer, but from what you say the design sounds probably flawed. If you have functions ( methods, whatever) identical except in that one respect then passing in a comparator, while better than passing a flag, is probably wrong. Refactor. You will probably end up with cleaner code. If not, all C descended languages and all OO languages allow passing comparators. <br />Ken Bhttps://www.blogger.com/profile/08207803092348071005noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-75083851857895049422014-12-12T22:53:27.681-05:002014-12-12T22:53:27.681-05:00It must suck to be a Lisp programmer with a lisp.It must suck to be a Lisp programmer with a lisp.Samson Corwellhttps://www.blogger.com/profile/10148822362930969284noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-60366598544534501022014-12-12T22:51:50.477-05:002014-12-12T22:51:50.477-05:00I tend prefer prettiness over optimization, so I a...I tend prefer prettiness over optimization, so I agree with you.Samson Corwellhttps://www.blogger.com/profile/10148822362930969284noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-7002229319726965002014-12-12T22:49:44.526-05:002014-12-12T22:49:44.526-05:00Here's the one using the ternary operator.
de...Here's the one using the ternary operator.<br /><br />def Comp(x, op, y):<br /> return (True if x>y else False) if op else (True if x<y else False)<br />Samson Corwellhttps://www.blogger.com/profile/10148822362930969284noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-8611689346889049002014-12-12T22:35:31.674-05:002014-12-12T22:35:31.674-05:00def Comp(x, op, y):
return eval(str(x)+op+str(...def Comp(x, op, y):<br /> return eval(str(x)+op+str(y))<br /><br />Tada!Samson Corwellhttps://www.blogger.com/profile/10148822362930969284noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-28636700122738946162014-12-12T22:26:28.523-05:002014-12-12T22:26:28.523-05:00Thank you. I just so happened to have used Python ...Thank you. I just so happened to have used Python to make sure I got the syntax right and learned its ternary operator is "<i>expr1</i> if <i>condition</i> else <i>expr2</i>" instead of "<i>condition</i> ? <i>expr1</i> : <i>expr2</i>". So, what I gave you would have to be converted over into that.<br /><br />If that's too much of an eyesore or a hassle, you can also modify the function take a string instead of a boolean parameter and pass it to "eval(str(x)+operator+str(y))".Samson Corwellhttps://www.blogger.com/profile/10148822362930969284noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-21819137437367104362014-12-12T20:26:47.460-05:002014-12-12T20:26:47.460-05:00Oy Matt, very clever!Oy Matt, very clever!gcallahhttps://www.blogger.com/profile/10065877215969589482noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-86711064229800794352014-12-12T20:24:15.384-05:002014-12-12T20:24:15.384-05:00Riffing: return x > y == gt
But in practice I ...Riffing: return x > y == gt<br /><br />But in practice I usually do the if statement so I don't have to scratch my head later!Matthttps://www.blogger.com/profile/02470489293820651609noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-79170956770616005132014-12-12T20:01:50.625-05:002014-12-12T20:01:50.625-05:00Nice coding, Samson!Nice coding, Samson!gcallahhttps://www.blogger.com/profile/10065877215969589482noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-86549340456222115002014-12-12T20:00:38.304-05:002014-12-12T20:00:38.304-05:00Matt, I wrote a lisp interpreter a number of years...Matt, I wrote a lisp interpreter a number of years ago: so I know about first class functions.<br /><br />But your Python code looks very interesting, and that is, indeed, the language I am working in.gcallahhttps://www.blogger.com/profile/10065877215969589482noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-34077521859266787552014-12-12T19:58:33.214-05:002014-12-12T19:58:33.214-05:00Yes, in Python also I can pass a function in: but ...Yes, in Python also I can pass a function in: but that is more code then I have now.gcallahhttps://www.blogger.com/profile/10065877215969589482noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-65280178074332748962014-12-12T19:40:36.748-05:002014-12-12T19:40:36.748-05:00return gt ? (x > y ? true : false) : (x < y ...return gt ? (x > y ? true : false) : (x < y ? true : false)Samson Corwellhttps://www.blogger.com/profile/10148822362930969284noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-47306091916190036822014-12-12T19:25:28.004-05:002014-12-12T19:25:28.004-05:00Programming languages are evolving to more commonl...Programming languages are evolving to more commonly support this kind of thing, called "first-class functions": http://en.wikipedia.org/wiki/First-class_function#Language_support<br /><br />It's usually at least possible to do it, but whether or not you can without resorting to silly gymnastics really depends on what language you're working in.<br /><br />If you're working in Python, you could use the 'operator' package: my_function(x, y, operator.lt) and my_function(x, y, operator.gt) for less than and greater than, respectively.Matthttps://www.blogger.com/profile/02470489293820651609noreply@blogger.comtag:blogger.com,1999:blog-7225373.post-5399991627721067612014-12-12T19:07:29.784-05:002014-12-12T19:07:29.784-05:00You want to use the parameter as the operator, rat...You want to use the parameter as the operator, rather than a value operated on. It is possible in C to pass a pointer to a function, receive the pointer as a parameter, and have the receiving code execute the function. <br /><br />So if you create your own set of operators as functions (or methods) I think you could achieve the objective you have in mind. Might be more trouble than it is worth.<br />The Arthurianhttps://www.blogger.com/profile/16501331051089400601noreply@blogger.com