{"action":"created","issue":{"url":"https://api.github.com/repos/Facepunch/garrysmod-requests/issues/527","labels_url":"https://api.github.com/repos/Facepunch/garrysmod-requests/issues/527/labels{/name}","comments_url":"https://api.github.com/repos/Facepunch/garrysmod-requests/issues/527/comments","events_url":"https://api.github.com/repos/Facepunch/garrysmod-requests/issues/527/events","html_url":"https://github.com/Facepunch/garrysmod-requests/issues/527","id":93081475,"number":527,"title":"Add basic math functionality as functions","user":{"login":"Acecool","id":4174280,"avatar_url":"https://avatars.githubusercontent.com/u/4174280?v=3","gravatar_id":"","url":"https://api.github.com/users/Acecool","html_url":"https://github.com/Acecool","followers_url":"https://api.github.com/users/Acecool/followers","following_url":"https://api.github.com/users/Acecool/following{/other_user}","gists_url":"https://api.github.com/users/Acecool/gists{/gist_id}","starred_url":"https://api.github.com/users/Acecool/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Acecool/subscriptions","organizations_url":"https://api.github.com/users/Acecool/orgs","repos_url":"https://api.github.com/users/Acecool/repos","events_url":"https://api.github.com/users/Acecool/events{/privacy}","received_events_url":"https://api.github.com/users/Acecool/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-07-05T06:03:14Z","updated_at":"2015-07-05T14:41:41Z","closed_at":"2015-07-05T14:41:41Z","body":"I was working on a new meta-table object and was adding all of the math functionality to it and as I was designing it, I saw that a lot of code would be repetitive for those segments because we don't have basic math functions as default in Lua or GLua...\r\n\r\n\r\nNote: Unpack converts \"x.y.z\" to return x, y, z\r\n\r\nBasically:\r\n\r\n```lua\r\nfunction version:__add( _version )\r\n\tlocal _major_c, _minor_c, _build_c\t= self:Unpack( );\r\n\tlocal _major_a, _minor_a, _build_a\t= _version:Unpack( );\r\n\treturn string.format( VERSION_FORMAT_STRING, _major_c + _major_a, _minor_c + _minor_a, _build_c + _build_a );\r\nend\r\n\r\nfunction version:__sub( _version )\r\n\tlocal _major_c, _minor_c, _build_c\t= self:Unpack( );\r\n\tlocal _major_a, _minor_a, _build_a\t= _version:Unpack( );\r\n\treturn string.format( VERSION_FORMAT_STRING, _major_c - _major_a, _minor_c - _minor_a, _build_c - _build_a );\r\nend\r\n\r\nfunction version:__mul( _version )\r\n\tlocal _major_c, _minor_c, _build_c\t= self:Unpack( );\r\n\tlocal _major_a, _minor_a, _build_a\t= _version:Unpack( );\r\n\treturn string.format( VERSION_FORMAT_STRING, _major_c * _major_a, _minor_c * _minor_a, _build_c * _build_a );\r\nend\r\n\r\nfunction version:__div( _version )\r\n\tlocal _major_c, _minor_c, _build_c\t= self:Unpack( );\r\n\tlocal _major_a, _minor_a, _build_a\t= _version:Unpack( );\r\n\treturn string.format( VERSION_FORMAT_STRING, _major_c / _major_a, _minor_c / _minor_a, _build_c / _build_a );\r\nend\r\n```\r\n\r\nis repetitive and can easily reduce to become\r\n\r\n```lua\r\nfunction version:__math( _version, _func )\r\n\tlocal _major_c, _minor_c, _build_c\t= self:Unpack( );\r\n\tlocal _major_a, _minor_a, _build_a\t= _version:Unpack( );\r\n\treturn string.format( VERSION_FORMAT_STRING, _func( _major_c, _major_a ), _func( _minor_c, _minor_a ), _func( _build_c, _build_a ) );\r\nend\r\n\r\nfunction version:__add( _version )\r\n\treturn self:__math( _version, math.add );\r\nend\r\n\r\nfunction version:__sub( _version )\r\n\treturn self:__math( _version, math.sub );\r\nend\r\n\r\nfunction version:__mul( _version )\r\n\treturn self:__math( _version, math.mul );\r\nend\r\n\r\nfunction version:__div( _version )\r\n\treturn self:__math( _version, math.div );\r\nend\r\n```\r\n\r\nOr even less depending on syntax chosen ( one line per math action ), or more with comments but less than needing to perform it all each time...\r\n\r\nBy adding:\r\n```lua\r\nfunction math.add( a, b ) return a + b; end\r\nfunction math.sub( a, b ) return a - b; end\r\nfunction math.mul( a, b ) return a * b; end\r\nfunction math.div( a, b ) return a / b; end\r\n-- math.mod exists...\r\n-- math.pow exists...\r\n```\r\n\r\nI'll submit a proper pull-request with the code later but I wanted to gauge thoughts right now before doing it because adding these is incredibly easy and I don't know how many people would use this.. I just prefer to slim my code down as much as possible and hate ever repeating anything unnecessarily.. \r\n\r\nI'm surprised those 4 simple math functions aren't included by default...\r\n\r\n\r\n. Currently we can not dynamically reference operators ( as far as I know ) but we can do so for functions...\r\n"},"comment":{"url":"https://api.github.com/repos/Facepunch/garrysmod-requests/issues/comments/118627736","html_url":"https://github.com/Facepunch/garrysmod-requests/issues/527#issuecomment-118627736","issue_url":"https://api.github.com/repos/Facepunch/garrysmod-requests/issues/527","id":118627736,"user":{"login":"robotboy655","id":3299036,"avatar_url":"https://avatars.githubusercontent.com/u/3299036?v=3","gravatar_id":"","url":"https://api.github.com/users/robotboy655","html_url":"https://github.com/robotboy655","followers_url":"https://api.github.com/users/robotboy655/followers","following_url":"https://api.github.com/users/robotboy655/following{/other_user}","gists_url":"https://api.github.com/users/robotboy655/gists{/gist_id}","starred_url":"https://api.github.com/users/robotboy655/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/robotboy655/subscriptions","organizations_url":"https://api.github.com/users/robotboy655/orgs","repos_url":"https://api.github.com/users/robotboy655/repos","events_url":"https://api.github.com/users/robotboy655/events{/privacy}","received_events_url":"https://api.github.com/users/robotboy655/received_events","type":"User","site_admin":false},"created_at":"2015-07-05T14:41:41Z","updated_at":"2015-07-05T14:41:41Z","body":"Can you stop with these ridiculous ideas please that would be very good."}}