Wednesday, February 27, 2013

Chrome CORS

This is a bit of a follow-up post to my Refused to set unsafe header post. A question I was asked was about how to do something with CORS in Chrome, and my immediate findings were that it is impossible. As of recently I looked back into it and have come across a couple possible solution.

The first possible solution is to make sure to send the proper headers, as Chrome will take into account what the server says is allowed. For this, you can set the access control headers however you want. For the example, I will show the php version I found.

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true ");
header("Access-Control-Allow-Methods: OPTIONS, GET, POST");
header("Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");

This will allow you to set the various headers you would need for a CORS request. Another possibility is by using a content type that the control isn't strictly enforced. With that, it seems by setting the Content-Type to plain/text, it should go through without a problem.

I personally haven't tried either of these because I have yet to have a use for it with what I do, however I wanted to share this bit of information should anyone be curious and happen across here.

Wednesday, February 20, 2013

Fibonacci Generator

I was reading up on various types of morphism recently. While I was disappointed in the examples, as they were showing off bad practice, even if they were just an example, I got an idea. While I do have a post showing off a similar bad example with calculating a Fibonacci number, I have since refined it into a faster and more efficient method and an explanation to it you can find here.

My main focus of interest was looking at catamorphism and anamorphism. In the process of reading up, I came across other types of morphisms that while were mathematically interesting, I only saw bad programming being shown emerging from the ideas.

To explain in the simplest way possible, catamorphism is a generator and the opposite of anamorphism, which is a fold. Generators are functions that produce data on-call, using the generated data to provide more data upon the next call. This can create an infinite data type or have an end. Counting can be catamorphic in nature. For the explanation, let's look at a Python generator.

def counting(x):
    while 1:
        yield x
        x += 1

This is the equivalent to this Haskell code.

counting x = [x..]

The Python code has a bit of a more robust description to it, but the idea is the same for both, they will count from x on up until forever. Anamorphism includes things like the sum function, a list of stuff goes in, and it's all folded together with a function (in the case of sum, adding) until one result remains.

Now to the cool part and what this all means. This all means that with the magic of generators, there is an easy way to create a fibonacci generator. This is a simple one-liner in Haskell and as far as I can tell, can outdo the crappy version of the recursive Fibonacci shown everywhere else.

fibgen a b = a : (fibgen b (a + b))

That's all there is to it. The idea is you supply the first two numbers and it does the rest, forever and ever. So if you want the numbers from it, just take them. Now I suppose I can be nice and give something similar in Python, although to be honest it will look pretty much like the counting function I made.

def fibgen(a, b):
    while 1:
        yield a
        a, b = b, a + b

With only some very small tweaking, I turned the counting function into a Fibonacci generator. Now, like magic, we have an easy way to cycle through a sequence of Fibonacci numbers. I want to try to work on various problems more in this manor for when list processing or other related activities that require sequential computations, I feel it simplifies the process and gives a more elegant solution (or if nothing else, it looks cool).

Sunday, February 10, 2013

Haskell: No instance for (Eq a)

I've been taking some time to dig a bit deeper into Haskell and was messing around with custom data types and records when I came across this little pesky error. After much Googleing and answer presenting itself, I decided to put my nose back into the ebook I was reading on Haskell and try to see if I could find what I was missing. Had I just scrolled a bit further down on some of the Haskell doc pages, my answer would have been staring me right in the face, as it did when I finally scrolled to the right part.

So while I thought I needed to qualify in some way that the datatype in my record would work with Eq, it turns out I just needed to qualify it with the instance declaration. So here's the erroring code:

instance Eq (MyType a) where

And now the fix. Ready for this complicated piece of work?

instance (Eq a) => Eq (MyType a) where

Then like magic, it decides all is right with the world and it works. From what I gather, it has to do with the pattern matching. I would show the rest of the code, but it's still a work in progress and my amature attempt at a binary tree datatype complete with functions for working it. If it works well, I'll show that off when I am done.

The code that I'm using it for is in my Code Dump, which is on my PyGopherd server. There's probably a few hundred better ways to do what I made, but when messing around with it in ghci, it did pretty well. Gonna be using my Gopher server as a dumping ground for code that I don't necessarily have a use for, but deem it okay enough to show others.

Wednesday, February 6, 2013

Python Partial Functions

One of the things I found to be rather cool when playing around with Haskell is partial function applications. With this, it was easy to pass a function to something for list processing, like folding, mapping or filtering.

After talking to someone who was making something in Perl, I was inclined to ask if he knew about currying functions or partial application, as he was taking advantage of how Perl handles and makes functions. Not really being a Perl programmer myself, I decided to look up currying in Python.

So after venturing into some of the PEP stuff for Python, I came across functools.partial(). The reason I think this is cool is because I don't like lambda functions in Python. While lambda functions are nice at first and allow you to do partial application in a sense, it's aggravating because any variables you use in it are in a the scope of the parent. This means, to isolate a lambda function, you would need to place it in a named function, thus defeating the purpose.

To give a rather bad example with no real application I can think of, consider this code.

>>> x = [lambda a: a*y for y in xrange(1, 10)]
>>> x[0](10)
>>> x[1](10)
>>> y
>>> y = 1
>>> x[2](10)

As you can see, everything results in the same because variables in list comprehension are also exposed to the global scope. So there are a few ways we can do this to get the results we expect. We can have a function that returns a function, either using lambda or nested functions. I prefer the nested function for something complicated, but for a simple thing like this, lambda will work as well.

>>> def a(b):
...  def c(d):
...   return d * b
...  return c
>>> x = [a(y) for y in xrange(1, 10)]
>>> x[0](10)
>>> x[1](10)
>>> def a(b):
...  return lambda c: c * b
>>> x = [a(y) for y in xrange(1, 10)]
>>> x[0](10)
>>> x[1](10)

Or we can do this with partial application, which I think would prove more versatile. The reason I think partial application could be more versatile is because there is no need to write a specialized function to return the function desired. So this next bit of code allows a general purpose function and no use of lambda, just a partial application.

>>> import functools
>>> def a(b, c):
...  return c * b
>>> x = [functools.partial(a, y) for y in xrange(1, 10)]
>>> x[0](10)
>>> x[1](10)

Those with sharp eyes may notice that I do b * c instead of c * b. This is because it does the partial application of variables in order. It does allow keyword arguments, which will allow you to pass arguments to a partial application in any order. Now while a lambda function in a single case would get the same results, remember that they still have an exposed scope.

So in the end, my favorite way to do this is with the partial application offered by functools. This is not to say lambdas and returning nested functions isn't good, as they do have their uses and lambda can make a quick and easy script for something small.

For more info, check these out:

Tag Cloud

.NET (1) A+ (2) addon (6) Android (4) anonymous functions (5) application (10) arduino (1) artificial intelligence (2) bash (4) c (7) camera (1) certifications (4) cobol (1) comptia (4) computing (2) css (2) customize (16) encryption (2) error (19) exploit (17) ftp (3) funny (2) gadget (3) games (2) Gtk (1) GUI (5) hardware (7) haskell (15) help (8) HTML (6) irc (2) java (5) javascript (21) Linux (20) Mac (5) malware (2) math (8) network (9) objects (2) OCaml (1) perl (4) php (9) plugin (7) programming (42) python (24) radio (1) regex (3) security (25) sound (1) speakers (1) ssh (3) story (1) Techs from the Crypt (2) telnet (2) tools (15) troubleshooting (5) Ubuntu (4) Unix (4) virtualization (1) web design (14) Windows (8) wx (2)